Reputation: 1967
I have some HTML like:
<input class="button" name="submit_button" type="submit" value="Abe Lincoln">
assuming a jQuery object
foo = $('input.button')
that consists of the above HTML, how can I extract the name from the 'value' attribute?
Upvotes: 0
Views: 124
Reputation: 268
Try this ;)
foo = $('input.button').val()
or you can use attr():
foo = $('input.button').attr('value')
Upvotes: 2
Reputation: 18891
$('input.button').val();
Docs: http://api.jquery.com/val/
.val()
will get the current value of the user's input. To get other attributes, use .attr()
:
$('input.button').attr('name') // returns "submit_button"
Docs: http://api.jquery.com/attr/
Note, also, a warning from the .attr
docs:
To retrieve and change DOM properties such as the
checked
,selected
, ordisabled
state of form elements, use the.prop()
method.
i.e., don't use $('input.button').attr('checked')
, rather $('input.button').prop('checked')
.
Docs: http://api.jquery.com/prop/
Upvotes: 3