JQuery

JQuery checkbox.val always on?

TAGs: 10 Comment

Posted By: eligeske on in JQuery

When using JQuery selectors and trying to get the value of a checkbox, it seems that it will always return the result “on”. That is because you need to check to see if it is “:checked” instead of trying to grab the value of it.

if($("input[name='YourCheckboxName']").is(":checked")){
alert('on');
}else{
alert('off');
}

10 Responses to "JQuery checkbox.val always on?"

"SHARING IS CARING"
  1. paul says:

    werwer

  2. Mark says:

    Ahh, this is what I suspected, thanks for the confirmation and example :)

  3. Mark says:

    And when you’re sending json data with an ajax request, it’s easy to just send the
    $(selector).is(“:checked”) instead of .val() when working with checkboxes

  4. Mathias says:

    Just thanks.

    I waste a lot of time to understand this, until I see your blog.

    Thanks

  5. eligeske says:

    Thanks for the props, I threw it together rather quickly. I used this web background maker for the images that I talk about in this post: http://eligeske.com/cool-web-stuff/fast-web-background-patterns/

  6. someone says:

    !!!! THANK YOU !!!!

  7. Marco says:

    Thanks a lot, I wasted several hours with debugging and researching on that problem. I assumed that (“&#35myCheckbox”).val() either returns “on” or “off”, depending on the status of the checkbox…

  8. JadedEvan says:

    Thanks for the tip, fixed my problem.

    Editorial: I don’t think the way this is implemented in jQuery is very elegant. Why do they design it so the checkbox needs all of this special handling? A checkbox can have no other values except for true/false, seems like the val() method should return that boolean state by default. *Groan*.

  9. eligeske says:

    Hey Evan,

    That is what the HTML value is, “on” if it is checked. So we should take this up with w3c!

  10. eligeske says:

    I’m right there with ya on that one Evan! You tell me when the rally is to change the value and I’ll be there.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Learning DHTMLX Suite UI

Learn the foundation of the DHTMLX Suite quickly while building a single page application with multiple components in harmony.

Popular post