Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call the button click event based on name property

Tags:

jquery

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?

like image 924
Manikandan Sethuraju Avatar asked Mar 14 '12 05:03

Manikandan Sethuraju


People also ask

Which one of the below method is used for Click event of a button to execute some action?

To make click event work add android:onClick attribute to the Button element in your XML layout. The value for this attribute must be the name of the method you want to call in response to a click event.

What is Click () method?

The click() method simulates a mouse-click on an element. This method can be used to execute a click on an element as if the user manually clicked on it.


2 Answers

You can use the name property for that particular element. For example to set a border of 2px around an input element with name xyz, you can use;

$(function() {
    $("input[name = 'xyz']").css("border","2px solid red");
})

DEMO

like image 172
defau1t Avatar answered Sep 28 '22 13:09

defau1t


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

in your case:

$('input[name="btnName"]').click(function(){
    //do stuff
});
like image 23
Joseph Avatar answered Sep 28 '22 14:09

Joseph