RuntimeException
RuntimeException

Reputation: 1135

Getting attribute name instead of value - query

Using this jquery code:

$("#targeting input:radio").each(function() {
   console.log ($(this).attr("selected"));
});

HTML:

 <input type="radio" id="dateFromTo" name="muScheduleDateRange" value="2" selected="someval"/>

In console.log I'm getting selected instead someval?

Upvotes: 1

Views: 92

Answers (3)

user229044
user229044

Reputation: 239301

selected is a boolean. You can't set it to anything except undefined or "selected". If you want to attach some value to the input, use the value attribute. If you want to attach some additional value to the input, use a data attribute.

Upvotes: 7

cssyphus
cssyphus

Reputation: 40038

Try this:

console.log($(this).prop('checked'));

Will return true or false, depending on whether the radio button is checked.

Source

Upvotes: 0

jvverde
jvverde

Reputation: 631

Try this

console.log($('#targeting input:radio:checked').val())

See jquery val

Upvotes: 0

Related Questions