Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery to check if an input has a specific name?

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

like image 816
Kamran Ahmed Avatar asked Nov 28 '22 21:11

Kamran Ahmed


2 Answers

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;
};
like image 181
ThiefMaster Avatar answered Dec 04 '22 10:12

ThiefMaster


Try this also

 var name = $("#id").attr("name");

OR

 var name = $("#id").prop("name");


if(name=="sr_column")
{
   //your code
}
like image 43
Amit Avatar answered Dec 04 '22 11:12

Amit