Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 22

Thread: if and

  1. #1
    Regular Coder
    Join Date
    May 2011
    Posts
    357
    Thanks
    23
    Thanked 1 Time in 1 Post

    if and

    I keep trying to make a if statment wiht two parameters but when i use && it does not work.

  • #2
    Regular Coder
    Join Date
    Sep 2011
    Location
    Sweden
    Posts
    154
    Thanks
    1
    Thanked 22 Times in 22 Posts
    Please, clearify - show us the code!

  • #3
    Regular Coder
    Join Date
    May 2011
    Posts
    357
    Thanks
    23
    Thanked 1 Time in 1 Post
    Quote Originally Posted by ironboy View Post
    Please, clearify - show us the code!
    It can just be a generic javascript if statment that has more than one parameter? How would you do that? i know theres a && "and" but it does not work.

  • #4
    Regular Coder
    Join Date
    Sep 2011
    Location
    Sweden
    Posts
    154
    Thanks
    1
    Thanked 22 Times in 22 Posts
    Code:
    var a = 1, b = 2;
    
    // This will trigger an alert
    if (a == 1 && b == 2){
      alert("Yo! That's cool');
    };
    
    a=2;
    
    // This won't
    if (a == 1 && b == 2){
      alert("Not!');
    };

  • #5
    Regular Coder
    Join Date
    Sep 2011
    Location
    Sweden
    Posts
    154
    Thanks
    1
    Thanked 22 Times in 22 Posts
    Which could also be written:
    Code:
    var a = 1, b = 2;
    
    // This will trigger an alert
    a == 1 && b == 2 && alert("Yo! That's cool');
    
    a=2;
    
    // This won't
    a == 1 && b == 2 && alert("Not!');

  • #6
    Regular Coder
    Join Date
    Aug 2010
    Posts
    945
    Thanks
    19
    Thanked 205 Times in 203 Posts
    a=1;b=1;

    if(a && b)
    alert('both a and b are "truthy"')
    else alert('either a or b or both are not "truthy"')

    if(a==true && b==true)
    alert('both a and b are "truthy"')
    else alert('either a or b or both are not "truthy"')
    Last edited by DaveyErwin; 10-05-2011 at 10:48 PM.

  • #7
    Regular Coder
    Join Date
    Sep 2011
    Location
    Sweden
    Posts
    154
    Thanks
    1
    Thanked 22 Times in 22 Posts
    Or like this:

    Code:
    var a = 1, b = 2;
    
    // This will trigger an alert
    a == 1 && b == 2 ? alert("Yo! That's cool') : 0;
    
    a=2;
    
    // This won't
    a == 1 && b == 2 ? alert("Not!') : 0;

  • #8
    Regular Coder
    Join Date
    Aug 2010
    Posts
    945
    Thanks
    19
    Thanked 205 Times in 203 Posts
    if(1 & 3)alert("true")//alerts true
    if(1 & 4)alert("true")//no alert

  • #9
    Regular Coder
    Join Date
    May 2011
    Posts
    357
    Thanks
    23
    Thanked 1 Time in 1 Post
    Does it have to be a var can it be a input field value?

  • #10
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Of course it can be an input field value. It can be anything.

    If you refuse to show us your code, we can't help you.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #11
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Quote Originally Posted by DaveyErwin View Post
    if(1 & 3)alert("true")//alerts true
    if(1 & 4)alert("true")//no alert
    Well, sure. But I thought this thread was about &&??

    if(1 && 3)alert("true")//alerts true
    if(1 && 4)alert("true")//also alerts true
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #12
    Regular Coder
    Join Date
    May 2011
    Posts
    357
    Thanks
    23
    Thanked 1 Time in 1 Post
    Code:
     if (document.getElementById("name").value == "Timothy Conner" && document.getElementById("relatea").value == "William Conner"){
    document.getElementById("relatec").value = "Brothers";
    };
    Well all of these are talking about input fields. It might not be the best explanation.

  • #13
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    That looks perfectly okay to me.

    Are you saying it doesn't work?

    Then you will have to show the HTML page that it is working with.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #14
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,355
    Thanks
    11
    Thanked 558 Times in 551 Posts
    Be careful with "dirty" user input! You should clean up the input before comparing it to any values using "=="

    Use a custom trim() method to cut off leading and trailing white spaces, then use toLowerCase() or toUpperCase() to avoid case ambiguities.

    Code:
    function trim(myString) {
       return myString.replace(/^\s+|\s+$/g, "");
    }
    
    var myName = trim(document.getElementById("name").value.toLowerCase());
    if (myName == "timothy conner") ...
    EDIT: didn't need the grouping parentheses in my regex
    Last edited by devnull69; 10-06-2011 at 08:08 AM.

  • #15
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,733
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    You might also wish to remove multiple spaces, and non-alpha characters except space hyphen apostrophe from the input. (myName = Mary-Lou O'Flanaghan).

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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