Reputation: 12438
Is there any function to check if an input has a specific name in jquery, like we check the existense of a class using hasClass()
?
For example if I have an input
<input type="checkbox" class="col_control" checked="checked" name="sr_column" data-columnno="0" />
so that I can check hasName("sr_column")
and it returns true
Upvotes: 4
Views: 15259
Reputation: 4881
Just for completeness, one can also use $.filter()
on the jQuery objects and check the length of the list, e.g.
var $inputs = $("input");
if ($inputs.filter("[name=text]").length > 0) {
// do something here
}
Upvotes: 0
Reputation: 318508
el.name == 'text'
No need for any jQuery! If you do have a jQuery object, use jq_el[0].name == 'text'
instead.
Of course you can also use jQuery to access this, using either jq_el.prop('name')
or jq_el.attr('name')
(it's available both as a property and an attribute).
If you want jq_el.hasName(...)
, you can define the function like this:
$.fn.hasName = function(name) {
return this.name == name;
};
Upvotes: 22
Reputation: 15387
Try this also
var name = $("#id").attr("name");
OR
var name = $("#id").prop("name");
if(name=="sr_column")
{
//your code
}
Upvotes: 4