Announcement Announcement Module
Collapse
No announcement yet.
Need some with with JQuery and an if/else if statement... Page Title Module
Move Remove Collapse
Search Search Module
Collapse

Advertisement Advertisement Module
Collapse

Featured Images Featured Images Module
Collapse

Mediabistro Creative Sites Mediabistro Creative Sites Module
Collapse
Latest Topics Latest Topics Module
Collapse

  • neeru_m
    Reply to Need help with logo
    neeru_m
    Thanks Amit for your suggestions. I'll try to improve my logo...
    Today, 04:22 AM
  • AmitMan
    Reply to Music album cover
    AmitMan
    I like the grunge style here. Its a little hard to read at times, especially with the white text on black.

    Nice work, keep up the good stuff!
    Today, 03:29 AM
  • AmitMan
    Reply to Need help with logo
    AmitMan
    I think I see what your trying to do with the underline, but I don't think its working in this case. A simple underline can help to add distance between a title and say a headline and can also make the...
    Today, 03:25 AM
  • AmitMan
    Reply to :::-----What's the graphic standards logo ------:::
    AmitMan
    I've heard of checklists and similar documents used for say ensuring your file is print ready (ensuring all digital images are at least 300dpi, file formats are corrects, appropriate bleeds etc.) but...
    Today, 03:11 AM
  • neeru_m
    Reply to Need help with logo
    neeru_m
    Sorry I just forget to mention. Its a Graphic and Web Design company logo. And the major symbol is a combination of "D" and "L" which is the first letter of company name "Design...
    Today, 02:57 AM
Advertisement Advertisement Module
Collapse

Sponsors Sponsors Module
Collapse

X
Conversation Detail Module
Collapse
  • 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, 09: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-15-2009, 01:19 AM.

              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

                   
                  home | site map | advertising/sponsorships | about us | careers | contact us | help courses | browse jobs | freelancers | events | forums | content | member benefits | reprints & permissions about | terms of use | privacy policy | Copyright © 2014 Mediabistro Inc. Mediabistro Inc. call (212) 389-2000 or email us
                  Working...
                  X