views:

109

answers:

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.

+1  A: 

Try this:

var mySecondVar = $("input[name='mySecondVar']:checked").length ? "Yes" : "No";
Nick Craver
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.
Mike Sherov
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 http://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?
Careless
@Careless - This is a ternary expression, just a different way of representing an if/else statement, here for some examples: http://snipplr.com/view/17704/ternary-expressions/ In your case the format `booleanThing ? valueIfTrue : valueIfFalse;` `.length` is 0 or false if there are no elements found, so the second value is retuned, if `.length` returned anything it would be over 0 or true, and the first value is returned in that case, make sense?
Nick Craver
A: 

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";
    }
JacobM
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.
Careless
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.
JacobM