2

I've successfully pulled the checked value from a radio button group with jQuery 1.4 using the following code:

var myFirstVar = $("input[name='myFirstVar']:checked").val();

How would I alter this code to find out whether a check-box was checked, and to change the value of the var based on whether it is checked or not.

I tried many things, but here is an example of what I thought would work:

    if ($("input[name='mySecondVar']:checked")) {
     var mySecondVar = "Yes";
    }
    else {
     var mySecondVar = "No";
    }

Any insight on simply changing the value whether the box is checked or not would be great.

3 Answers 3

2

Try this:

var mySecondVar = $("input[name='mySecondVar']:checked").length ? "Yes" : "No";
Sign up to request clarification or add additional context in comments.

2 Comments

good use of the jQuery .length attribute. Keep in mind this is the jQuery .length property, and not a native javascript array property, considering $("input[name='mySecondVar']:checked") returns a jQuery object, not an array.
Nick, I've used your code and it worked by just changing my variable names. Thank you very much. However, I do not understand what is happening with this .length ? "Yes" : "No"; attribute. The demonstration at api.jquery.com/length is a little over my head. Am I right to assume that for the .length attribute, whatever is before the ':' is the set value for the "more than 0" characters length and whatever comes after would be the set value for a "0" characters length?
1

The ":checked" part of the selector is just telling jQuery to only select items that are checked. jQuery selectors always return a result (the jQuery object), so simply putting the selector in the if clause isn't sufficient. You want to see whether the result actually included any elements, so $("input[name='mySecondVar']:checked").length will work for your conditional clause (since Javascript interprets 0 as "false").

Another approach would be

if ($("input[name='mySecondVar']")[0].checked) {
     var mySecondVar = "Yes";
    }
    else {
     var mySecondVar = "No";
    }

2 Comments

Hi JacobM, Thank you for contributing. Not being a developer, through and through... this one makes more sense to me than the previous one because what is happening is laid out a lot more verbosely. However, I went with Nick's contribution and it seemed to work, so I'm going to stick with that. Much appreciated.
Absolutely. Nick's approach is called the "ternary operator" and is a shorter syntax for simple if-then statements; basically CONDITION ? TRUE ANSWER : FALSE ANSWER. I often use it in real life, but for an SO answer I think the explicit if-then is clearer. The functionality is identical.
0

You can set your java script variable's based on condition.

if($("input[name='testVar']:checked").length)
  set variable
else
  set variable

Comments

Your Answer

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