Nesting ifs?

Discussion in 'Javascript' started by Garry Jones, Oct 6, 2006.

  1. Garry Jones

    Garry Jones Guest

    I want to trigger an alert if three conditons are met.

    How do I nest if statements? I tried this, but it triggered the alert if
    just one of the three conditions was met.

    function checkform ( form )
    {
    if (form.blue.value == "yes") {
    if (form.green.value == "yes") {
    if (form.yellow.value == "yes") {
    alert( "You can only choose two items for this option");
    form.dtet1.focus();
    return false ;
    }
    }
    }
    return true ;
    }

    Any help greatly appreciated

    Garry Jones
    Sweden
    Garry Jones, Oct 6, 2006
    #1
    1. Advertising

  2. Garry Jones

    Touffy Guest

    On 2006-10-06 16:22:35 +0200, "Garry Jones" <> said:

    > I want to trigger an alert if three conditons are met.


    you could just combine them in a single if, which would be more
    efficient, and would save a lot of {}'s :

    if(form.blue.value == "yes" && form.green.value == "yes"
    && form.yellow.value == "yes")

    > function checkform ( form )
    > {
    > if (form.blue.value == "yes") {
    > if (form.green.value == "yes") {
    > if (form.yellow.value == "yes") {
    > alert( "You can only choose two items for this option");
    > form.dtet1.focus();
    > return false ;
    > }
    > }
    > }
    > return true ;
    > }


    your code seems correct though. Can't see why it fails. Maybe the
    values of your form inputs are initially set to "yes" ?


    --
    David Junger
    Touffy, Oct 6, 2006
    #2
    1. Advertising

  3. Garry Jones wrote:

    [snip]

    > How do I nest if statements?


    Precisely the way you posted, though in the posted case, you can just
    use a single if statement and combine the conditions with the logical
    AND operator (&&).

    > I tried this, but it triggered the alert if just one of the three
    > conditions was met.


    [snip]

    > if (form.blue.value == "yes") {


    Is the control a checkbox? If so, it's value will always be the same
    regardless of whether the control is checked. You should check the
    checked property.

    > if (form.green.value == "yes") {
    > if (form.yellow.value == "yes") {


    Another consideration is that if all of these represent values within a
    common category (colours, here), give the checkboxes the same name[1]
    and use their values to indicate the option chosen:

    <input name="colour" type="checkbox" value="blue">
    <input name="colour" type="checkbox" value="green">
    <input name="colour" type="checkbox" value="yellow">

    To check the number of checked controls within that group, loop through
    the resulting collection:

    function countCheckedControls(controlGroup) {
    var count = 0;

    for (var i = 0, n = controlGroup.length; i < n; ++i)
    if (controlGroup.checked) ++count;
    return count;
    }

    var checkedColours = countCheckedControls(form.elements.colour);

    [snip]

    Mike


    [1] For PHP, the name would be "colour[]". This will create an
    array within the $_GET superglobal array with the name
    "colour". Each element of this will contain the values of the
    checked checkboxes. To use that name in a script, use bracket
    notation:

    form.elements['colour[]']
    Michael Winter, Oct 6, 2006
    #3
  4. Garry Jones

    Garry Jones Guest

    "Touffy" <> skrev i meddelandet
    news:452668b2$0$1504$...
    > On 2006-10-06 16:22:35 +0200, "Garry Jones" <> said:
    >
    >> I want to trigger an alert if three conditons are met.

    >
    > you could just combine them in a single if, which would be more efficient,
    > and would save a lot of {}'s :


    Thanks for your answers, problem persisting.

    I simplified my code to give you guys a simple example. I put the && in my
    code but it is still misfiring. This time I post the actuall code.

    The values come from several checkboxes which come from different groups of
    checkboxes. I need to check for a certain conditon where five checkboxes
    have been clicked. Checkboxes that have been clicked pass on the value of
    "ja" . This works on the subsequent processing form and I can sucessfully
    test for all "ja" values there and unchecked boxes are not returning "ja". I
    can back the user from the processing form but would prefer to catch this
    condition with java script.

    They do not form part of an array because they come from different
    selections of buttons in other catergoized groups. So why is this not
    working? It never triggers the alert.

    if(form.dtet1.value == "ja" && form.bil1.value == "ja" && form.mmm33.value
    == "ja" && form.kitt2.value == "ja" && form.dtet2.value == "ja")
    alert( "warning text" );
    form.dtet1.focus();
    return false ;

    This is an example of one of the checkboxes.
    <input name="dtet1" type="checkbox" class="dt_inp" id="dtet1" title="dtet1"
    value="ja">

    This returns "ja" when clicked. But how do I trap those 5 values with java?

    Any help very greatfully appreciated.

    Garry Jones
    Sweden
    Garry Jones, Oct 7, 2006
    #4
  5. Garry Jones

    Touffy Guest

    Garry Jones said:

    > The values come from several checkboxes which come from different
    > groups of checkboxes. I need to check for a certain conditon where five
    > checkboxes have been clicked. Checkboxes that have been clicked pass on
    > the value of "ja" . This works on the subsequent processing form and I
    > can sucessfully test for all "ja" values there and unchecked boxes are
    > not returning "ja". I can back the user from the processing form but
    > would prefer to catch this condition with java script.


    How do you determine that they do not return "ja" when not checked ?
    Do you conclude that just because the server receives a form result
    without the values of unchecked boxes ?

    Under certain conditions, a form field will not be sent to the form's
    action. That happens when the field is disabled, unchecked (for
    checkboxes), or not selected (radios and select options).
    Not being sent doesn't prevent it from being an HTML element with
    attributes that can be accessed by client-side scripts.

    > if(form.dtet1.value == "ja" && form.bil1.value == "ja" &&
    > form.mmm33.value == "ja" && form.kitt2.value == "ja" &&
    > form.dtet2.value == "ja")
    > alert( "warning text" );
    > form.dtet1.focus();
    > return false ;
    >
    > This is an example of one of the checkboxes.
    > <input name="dtet1" type="checkbox" class="dt_inp" id="dtet1"
    > title="dtet1" value="ja">


    This code clearly sets the value of the checkbox to "ja". Look no further.
    What you have to test is whether the checkbox's checked attribute is
    true, not whether its value is "ja".

    Replace every '.value == "ja"' in your test by '.checked == true' (or
    just '.checked') and it should work.

    > This returns "ja" when clicked. But how do I trap those 5 values with java?


    with Java, I have no idea. We're talking JavaScript, right ?


    --
    David Junger
    Touffy, Oct 7, 2006
    #5
  6. Garry Jones

    Garry Jones Guest

    Touffy" <> skrev i meddelandet
    news:452790e6$0$24061$...

    > How do you determine that they do not return "ja" when not checked ?


    Because on the processing form I use this code on those values.

    if ($var == "ja"){
    $dtmoney = $dtmoney + 350;
    }

    Where $var is each and everyone of those values.

    > Do you conclude that just because the server receives a form result
    > without the values of unchecked boxes ?


    Well, yes the variable $dtmoney fires correctly and only adds 350 with the
    checkboxes that have been checked. ie it is those and only those boxes that
    pass the value of ja. The other checkboxes return null.

    > This code clearly sets the value of the checkbox to "ja". Look no further.
    > What you have to test is whether the checkbox's checked attribute is true,
    > not whether its value is "ja".


    Thats new to me. As far as php is concerned the value is only "ja" if the
    checkbox is checked. I thought javascript would treat this in the same way
    and I can not understand how Javascript sees a "ja" when php sees a "null".
    Obviously it does so I am pleased that you have taking the time to explain
    this to me.

    > Replace every '.value == "ja"' in your test by '.checked == true' (or just
    > '.checked') and it should work.


    Ok, great, thanks for your help. I will do that now.

    Garry Jones
    Sweden
    Garry Jones, Oct 7, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    2
    Views:
    2,935
    Peter
    Sep 30, 2005
  2. Continental Translations

    Drop Down Ifs

    Continental Translations, Jun 9, 2004, in forum: HTML
    Replies:
    4
    Views:
    432
    Continental Translations
    Jun 10, 2004
  3. Markus Dehmann
    Replies:
    8
    Views:
    2,080
    bartek
    Jun 2, 2004
  4. Vasily

    Optimisation of nested IFs

    Vasily, Jul 27, 2006, in forum: Java
    Replies:
    4
    Views:
    301
    Vasily
    Jul 28, 2006
  5. Trans
    Replies:
    10
    Views:
    286
    Sean O'Halpin
    Sep 16, 2005
Loading...

Share This Page