1

I do not understand why it still does not pass the form, even if I choose another checkbox? According to the if-else action, if else is empty, then do it

HTML:

 <form action="http://youtube.com" method="get">
  <ul>
    <li>
      <input id="checkbox_yoda" type="checkbox" name="character" value="light_side">
      <label for="checkbox_yoda" data-sentence="Force is strong in you">Yoda</label>
    </li>
    <li>
      <input id="checkbox_trooper" type="checkbox" name="character" value="dark_side">
      <label for="checkbox_trooper" data-sentence="Just chillin'">Trooper</label>
    </li>
    <li>
      <input id="checkbox_vader" type="checkbox" name="character" value="dark_side">
      <label for="checkbox_vader" data-sentence="There is no escape from the Dark Side.">Vader</label>
    </li>
  </ul>
  <button type="submit">Turn to the dark side</button>
</form>

JQUERY:

$("document").ready (
    function()
    {
        $("button").click(
            function()
            {
                if ($("input#checkbox_yoda :selected"))
                {
                    alert('you pick yoda');
                    return false;
                }

                else
                {
                    return true;
                }
            }
        )
    }
)
5
  • 1
    You are checking if the object exists (which it always does by using jquery selector) and not if it is selected. Commented May 9, 2018 at 13:48
  • 6
    $() returns a jQuery object. And objects in javascript are truthy. If you put .length on the end of it, it will return 0 if the element is not found, and 0 is falsey Commented May 9, 2018 at 13:48
  • Also remove the space between the input selector and :checked. The space signifies a child selector, which I'm assuming you do not want. Commented May 9, 2018 at 13:49
  • or you could use $('#checkbox_yoda').is(':checked') - if it is a checkbox - :selected is for dropdownlists Commented May 9, 2018 at 13:50
  • 1
    Kinda sad how people hunt points on typo/nonrepro/already answered in comments long ago questions.. Commented May 9, 2018 at 14:36

2 Answers 2

1

Checkbox has :checked selector so use is(':checked') to determine if the checkbox was checked or unchecked.

$("document").ready (
  function()
  {
    $("button").click(
      function()
      {
        if ($("input#checkbox_yoda").is(':checked'))
        {
          alert('you pick yoda');
          return false;
        }

        else
        {
          return true;
        }
      }
    )
  }
)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form action="http://youtube.com" method="get">
  <ul>
    <li>
      <input id="checkbox_yoda" type="checkbox" name="character" value="light_side">
      <label for="checkbox_yoda" data-sentence="Force is strong in you">Yoda</label>
    </li>
    <li>
      <input id="checkbox_trooper" type="checkbox" name="character" value="dark_side">
      <label for="checkbox_trooper" data-sentence="Just chillin'">Trooper</label>
    </li>
    <li>
      <input id="checkbox_vader" type="checkbox" name="character" value="dark_side">
      <label for="checkbox_vader" data-sentence="There is no escape from the Dark Side.">Vader</label>
    </li>
  </ul>
  <button type="submit">Turn to the dark side</button>
</form>

Sign up to request clarification or add additional context in comments.

Comments

0

As you've got a multiple choice, you may want to do something like this:

$("document").ready(
  function() {
    $("button").click(
      function() {
        $("input[type=checkbox]").each(function() {
          if ($(this).is(':checked')) {
            alert('You picked ' + $(this).siblings('label').html() + '.');
          }
        })
      }
    )
  }
)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form action="http://youtube.com" method="get">(Try selecting more than one)
  <ul>
    <li>
      <input id="checkbox_yoda" type="checkbox" name="character" value="light_side">
      <label for="checkbox_yoda" data-sentence="Force is strong in you">Yoda</label>
    </li>
    <li>
      <input id="checkbox_yobiwan" type="checkbox" name="character" value="light_side">
      <label for="checkbox_obiwan" data-sentence="">Obiwan</label>
    </li>
    <li>
      <input id="checkbox_trooper" type="checkbox" name="character" value="dark_side">
      <label for="checkbox_trooper" data-sentence="Just chillin'">Trooper</label>
    </li>
    <li>
      <input id="checkbox_vader" type="checkbox" name="character" value="dark_side">
      <label for="checkbox_vader" data-sentence="There is no escape from the Dark Side.">Vader</label>
    </li>
    <li>
      <input id="checkbox_luke" type="checkbox" name="character" value="light_side">
      <label for="checkbox_luke" data-sentence="">Luke</label>
    </li>
  </ul>
  <button type="submit">Turn to the dark side</button>
</form>

Hope it helps.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.