dllhell
dllhell

Reputation: 2014

how to get checked value from radiobutton represented as variable?

I would like to get checked value of some radiubutton. That is not a problem when you using expression:

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

but I have a situation when I need to use DOM object from variable as below:

var obj = $("[name=myradio1]");

and now I'm operating with variable obj.

Please explain how to get a checked value from obj variable

this is html code:

<input id="answer_21" type="radio" value="21" name="answer[1]">
<input id="answer_22" type="radio" value="22" name="answer[1]">

Upvotes: 0

Views: 982

Answers (2)

dllhell
dllhell

Reputation: 2014

alright... this works, I don't know can it be written better, probably can...

if (obj.is(':checked')){

        for (a=0; a<obj.length; a++)
            {
            if(obj[a].attributes[3].ownerElement.checked)
                {
                console.log(obj[a].attributes[3].ownerElement.defaultValue)  ;
                }

            }
        }

Upvotes: 0

Sarfraz
Sarfraz

Reputation: 382881

Update

if (obj.is(':checked')){
  var val = obj.val();
}

Please explain how to get a checked value from obj variable

You can use is() and :checked filter selector like this:

var isChecked = obj.is(':checked');

Upvotes: 1

Related Questions