[jquery] JQuery - Call the jquery button click event based on name property

I have one HTML button like,

<input type="button" id="btnSubmit" name="btnName" class="btnclass" value="Click Me" />

I want to call JQuery button click event based on id property means, we use the code like,

$("#btnSubmit").click(function(){ 
    ////////
});

as well as call button click event based on class property means, we use the code like,

$(".btnclass").click(function(){
    /////////
});

and My question is, I want to call the button click event based on name property means, How to do this?

This question is related to jquery

The answer is


You have to use the jquery attribute selector. You can read more here:

http://api.jquery.com/attribute-equals-selector/

In your case it should be:

$('input[name="btnName"]')

$('element[name="element_name"]').click(function(){
    //do stuff
});

in your case:

$('input[name="btnName"]').click(function(){
    //do stuff
});

You can use normal CSS selectors to select an element by name using jquery. Like this:

Button Code
<button type="button" name="mybutton">Click Me!</button>

Selector & Event Bind Code
$("button[name='mybutton']").click(function() {});