简体   繁体   中英

Jquery Passing this as function parameter

HTML

<input type="button" id="1" class="add" value="+" title="Add" onclick="set(this);"/>

JS

function set(obj){
   alert(obj.id);
}

The above code is not working.

What I Require

I need to pass the id attribute of the button in the onclick function. Is there any way I could achieve this.

Your html should be

 <input type="button" id="1" class="add" value="+" title="Add" onclick="set(this.id);"/>

and write java script function as

function set(id)
   {
   alert(id);
   }

Your title mentions jQuery, but you're working with a JS solution. If you want to do this with jQuery:

HTML:

<input type="button" id="1" class="add" value="+" title="Add" />

JS:

$("#1").click(function(){alert(this.id);});

Fiddle

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM