Announcement

Collapse
No announcement yet.

Need some with with JQuery and an if/else if statement...

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Need some with with JQuery and an if/else if statement...

    I'm new to jQuery and I'm trying to set a variable when a link is clicked. Then when another link is clicked, it reads the variable and, using an if/else if statement, runs the appropriate code.

    I'm not sure I have the correct syntax for some of these things. Any help would be awesome.
    Code:
        $(function() {
            $('a.click_rotors').click(function() {
                 if ($current_link = "bases1") {
                 $('#bases1').fadeOut(function () {
                     $('#rotors1').fadeIn();
                 });
                 } else {
                 $('#onesies').fadeOut(function () {
                     $('#rotors1').fadeIn();
                 });
                 };               
                 $current_link = "rotors1";
            });<!--close click function-->
    
            $('a.click_bases1').click(function() {
                 if ($current_link = "rotors1") {
                 $('#rotors1').fadeOut(function () {
                     $('#bases1').fadeIn();
                 });
                 } else {
                 $('#onesies').fadeOut(function () {
                     $('#bases1').fadeIn();
                 });
                 };             
                 $current_link = "bases1";
            });<!--close click function-->
    
            $('a.click_onesies').click(function() {
                 if ($current_link = "rotors1") {
                 $('#rotors1').fadeOut(function () {
                     $('#onesies').fadeIn();
                 });
                 } else {
                 $('#bases1').fadeOut(function () {
                     $('#onesies').fadeIn();
                 });  
                 };             
                 $current_link = "onesies";
            });<!--close click function-->
        });

  • #2
    A little clarification...this is working except when I click the "onesies" link, then click one of the other links. The if statement should read that the variable "current_link" has a value of "onesies", then run the "else" portion of the if/else statement. But it runs the "if" portion. So something must be wrong with the setting of my variable, and reading it in the if/else statement.

    Comment


    • #3
      Using some alert statements, it looks like my variables are being written and changed correctly when the links are pressed, so the problem must have something to do with the if/else statement

      Edit: I've been doing some more debugging and it is running the first if statement even if it is not true, rather than the else statement. Anyone know why?
      Last edited by MikeTheVike; 09-14-2009, 05:00 PM.

      Comment


      • #4
        Are you using any other js add ones for this? I was on my site and 1 add on (prototype.js) was canceling out all of my jQuery scripts.

        Sorry I can't be of much more help...
        Professional Pixel Pusher — Designing the world around you. | Working daily to reach 10,000 hours of practice.

        Comment


        • #5
          it's because you're just assigning "bases1" to $current_link. to test for equality you have to use ==.
          portfolio | daily image | reffee

          Comment


          • #6
            HTML Code:
                $(function() {
                
                	/*
                	* To be accessible to all functions
                	* current link must be defined here.
                	*/
                	var current_link = '';
                
                    $('a.click_rotors').click(function() {
                         if (current_link == "bases1") {
                         $('#bases1').fadeOut(function () {
                             $('#rotors1').fadeIn();
                         });
                         } else {
                         $('#onesies').fadeOut(function () {
                             $('#rotors1').fadeIn();
                         });
                         };               
                         current_link = "rotors1";
                    });
            
                    $('a.click_bases1').click(function() {
                         if (current_link == "rotors1") {
                         $('#rotors1').fadeOut(function () {
                             $('#bases1').fadeIn();
                         });
                         } else {
                         $('#onesies').fadeOut(function () {
                             $('#bases1').fadeIn();
                         });
                         };             
                         current_link = "bases1";
                    });
            
                    $('a.click_onesies').click(function() {
                         if (current_link == "rotors1") {
                         $('#rotors1').fadeOut(function () {
                             $('#onesies').fadeIn();
                         });
                         } else {
                         $('#bases1').fadeOut(function () {
                             $('#onesies').fadeIn();
                         });  
                         };             
                         current_link = "onesies";
                    });
                });

            Comment


            • #7
              You can try this out for a simplified version. Although, there is probably a syntax error somewhere.

              HTML Code:
              $(function() {
              	
              	var funcOnClick = 
              	(function() {
              	
              		var objCurrent = null;
              	
              		return function(objThis) {
              			if(!objCurrent) {
              				objThis.fadeIn(function() {
              					objCurrent = objThis;
              				});
              			} else if(objCurrent !== objThis) {
              				objCurrent.fadeOut(function() {
              					objThis.fadeIn(function() {
              						objCurrent = objThis;
              					});
              				});
              			}
              		}
              	
              	})();
              	
              	var objRotors = $('a.click_rotors');
              	var objBases1 = $('a.click_bases1');
              	var objOnsies = $('a.click_onesies');
              	
              	objRotors.click(function() {
              		funcOnClick(objRotors);
              	});
              	
              	objBases1.click(function() {
              		funcOnClick(objBases1);
              	});
              	
              	objOnsies.click(function() {
              		funcOnClick(objOnsies);
              	});
              
              });
              Tracking state like this has to do with closures not JQuery. The short verison is that a function in JavaScript has access to all variables defined outside of it. However, it does not have direct access to variables within nested functions - only things above. This makes it possible to bind a variable to a function explicitly then change it to track state. That is essentially what is happening with currentLink in the above code. The variable currentLink is bound to the returned function. making it possible to change it from within the scope of that returned function. Yet, keep it entirely out of the scope of everything besides that function. maybe some of that made some sense…

              It took a me a long time to understand closures and binding. However, once you do its a very powerful concept.
              Last edited by tZ; 09-14-2009, 09:19 PM.

              Comment


              • #8
                Originally posted by Two-Toe Tom View Post
                it's because you're just assigning "bases1" to $current_link. to test for equality you have to use ==.

                Thanks, its funny because I know that is how it is in Actionscript 3.0, but everything I found online never said anything about using two equal signs. It worked, thanks!

                Comment


                • #9
                  Originally posted by tZ View Post
                  You can try this out for a simplified version. Although, there is probably a syntax error somewhere.

                  HTML Code:
                  $(function() {
                  	
                  	var funcOnClick = 
                  	(function() {
                  	
                  		var objCurrent = null;
                  	
                  		return function(objThis) {
                  			if(!objCurrent) {
                  				objThis.fadeIn(function() {
                  					objCurrent = objThis;
                  				});
                  			} else if(objCurrent !== objThis) {
                  				objCurrent.fadeOut(function() {
                  					objThis.fadeIn(function() {
                  						objCurrent = objThis;
                  					});
                  				});
                  			}
                  		}
                  	
                  	})();
                  	
                  	var objRotors = $('a.click_rotors');
                  	var objBases1 = $('a.click_bases1');
                  	var objOnsies = $('a.click_onesies');
                  	
                  	objRotors.click(function() {
                  		funcOnClick(objRotors);
                  	});
                  	
                  	objBases1.click(function() {
                  		funcOnClick(objBases1);
                  	});
                  	
                  	objOnsies.click(function() {
                  		funcOnClick(objOnsies);
                  	});
                  
                  });
                  Tracking state like this has to do with closures not JQuery. The short verison is that a function in JavaScript has access to all variables defined outside of it. However, it does not have direct access to variables within nested functions - only things above. This makes it possible to bind a variable to a function explicitly then change it to track state. That is essentially what is happening with currentLink in the above code. The variable currentLink is bound to the returned function. making it possible to change it from within the scope of that returned function. Yet, keep it entirely out of the scope of everything besides that function. maybe some of that made some sense…

                  It took a me a long time to understand closures and binding. However, once you do its a very powerful concept.

                  Thanks tZ, I thought there had to have been a simpler way. I'll give it a try.

                  Comment

                  Search

                  Collapse

                  Sponsor

                  Collapse

                  Incredible Stock

                  Latest Topics

                  Collapse

                  • ellisgraphics
                    Help me?
                    ellisgraphics
                    I've got a client that designed his own logo and simply wanted me to put it together. He only drew the outline and told me what colors to use. But I'm not sure if I can send this to him. Should I tell...
                    Today, 07:17 PM
                  • exaus
                    Comment on Logo collection critique
                    exaus
                    Thanks for your feedback!
                    I understand what you are saying. I just think i might be a monomaniac. I have no interest in building in other forms of graphic design. I will probably have a bad time....
                    Today, 04:59 PM
                  • toweln
                    Book cover critique for a total beginner
                    toweln
                    Hi, I'm very new to design and just starting out, so I'd appreciate some critique/advice/guidance on some book covers I made for a friend, and how I can improve as a designer.


                    ...
                    Today, 04:58 PM
                  • HotButton
                    Reply to Logo collection critique
                    HotButton
                    Well, for one thing, logo design is a terrible entry point for a new designer trying to find clients and establish themselves. There are excellent designers with years of experience who avoid logo design...
                    Today, 04:37 PM
                  • exaus
                    Comment on Logo collection critique
                    exaus
                    I do not have a degree in Graphic Design. My goal is to work freelance.
                    This is what I am scared of since the country I live in does not respect design that much. You can rarely make a living out...
                    Today, 04:35 PM
                  GDF A division of Mediabistro Holdings Adweek | Mediabistro | Clio | Film Expo Group Contact Us | Terms of Use | Privacy Policy Copyright © 2016 Mediabistro Holdings
                  Working...
                  X