www.webdeveloper.com
Results 1 to 9 of 9

Thread: If/Else Statement

  1. #1
    Join Date
    Jan 2011
    Posts
    15

    If/Else Statement

    Hello,

    I'm not too familiar with coding for javascript, so I don't know how to properly write an if/else statement. The code below basically moves a div tag down the page when the button headerOp is clicked.

    //-HeaderAd starts

    $('div#headerOp').click(

    function() {
    $('#headerContent').animate({"marginTop": "+=60px"}, "slow");
    $(this).animate({"marginTop": "=60px"}, "slow").animate({"marginTop": "=5px"}, "fast");

    $('#TotalBodyId').animate({"marginTop": "+=60px"}, "slow");
    $('#TotalBodyId').css({backgroundPosition: "0 0"});

    });

    I need to write an else statement that does a returnFalse() if marginTop is greater than or equal to 60px. Can anyone help me with this?

    Thanks!
    Last edited by Kor; 02-07-2011 at 08:34 AM. Reason: using the JQuery prefix

  2. #2
    Join Date
    Sep 2009
    Posts
    146
    wow, i didnt know you could do the +=60px... thanks!

    as for your if, im not sure if you want the 60px check on "TotalBodyId", "headerContent"or "headerOp" but here is the code:
    Code:
    if ( $(ELEMENT).css("margin-top").replace("px", "") >= 60 ) {
    } else {
    }
    notice that you have to strip out the "px" from the returned margin-top otherwise its a string and numeric comparisons wont work. (if it gives you problems try wrapping it in parseInt($(ELEMENT).css("margin-top").replace("px", "")) )

    good luck,
    -aPeg

  3. #3
    Join Date
    Jan 2011
    Posts
    15
    Sorry if I'm really slow, but can you show me how I would implement this into my code? I tried this, but I have a syntax error in there somewhere.........

    Code:
    	
    $('div#headerOp').click(
    
    f ( $('#headerContent').css("margin-top").replace("px", "") <= 60 ) {
    		
    		function() {
    			$('#headerContent').animate({"marginTop": "+=60px"}, "slow");
    			$(this).animate({"marginTop": "=60px"}, "slow").animate({"marginTop": "=5px"}, "fast"); 
    			
    			$('#TotalBodyId').animate({"marginTop": "+=60px"}, "slow");
    			$('#TotalBodyId').css({backgroundPosition: "0 0"});
    
    }
    			
    		});
    I want to check headerContent, so I replaced ELEMENT with that. Tell me if I'm wrong, but I also don't think I need the "else" if I make the check "<= 60" instead of ">=60" This would basically only perform the function if marginTop is less than 60px, right?

  4. #4
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    Quote Originally Posted by k.croft
    I'm not too familiar with coding for javascript...
    Are you conscious that you are using a JQuery syntax, not the native JavaScript syntax? Do you know what is a javascript framework (like JQuery)? Do you know that JQuery is not the only framework (there are several important ones).

    Whenever you have a problem regarding a framework, please, specify which is the framework you are working with. Don't let us guess.

  5. #5
    Join Date
    Jan 2011
    Posts
    15
    Sorry........ I am aware that I am coding for JQuery, but I did warn you that I was inexperienced. And just because you are protected by the anonymity of the web doesn't mean you can be more rude to someone online that you would in person.....

  6. #6
    Join Date
    Dec 2002
    Location
    St. Louis, MO, USA
    Posts
    1,582
    I don't think KOR was being necessarily rude; emotions are difficult to determine in a typed medium, like this.

    ^_^

  7. #7
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    Quote Originally Posted by k.croft View Post
    Sorry........ I am aware that I am coding for JQuery, but I did warn you that I was inexperienced.
    That is not an excuse. There are many JavaScript libraries, each with its own classes, syntax, etc. We can not guess from a first shot.
    Quote Originally Posted by k.croft View Post
    And just because you are protected by the anonymity of the web doesn't mean you can be more rude to someone online that you would in person.....
    I was not rude. I was asking you to be precise. And I am less anonymous than you are, as I am moderator here.

  8. #8
    Join Date
    Jan 2011
    Posts
    15
    Well then I apologize for the misunderstanding. But here is some food for thought.... as a moderator, a leader on this forum, people will be much more receptive to your suggestions if you find a less critical way to say them. After all, this is a help forum.

  9. #9
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    Quote Originally Posted by k.croft View Post
    Well then I apologize for the misunderstanding. But here is some food for thought.... as a moderator, a leader on this forum, people will be much more receptive to your suggestions if you find a less critical way to say them. After all, this is a help forum.
    I really don't understand what bothers you. I was not critical, I have only put some questions and emphasized some less knows issues. Case closed.

    Now, regarding your problem. Have you studied the JQuery documentation?

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles