dt1000
dt1000

Reputation: 3732

get id of button in button handler javascript

I'm updating a page through ajax, so I'm creating (text) buttons dynamically. I know how to set up an onclick event, but then how to I get the id of the button in the handler?

Using sinatra with ruby and jquery 1.5. Here is the partial code:

<div onclick='my_handler()' ><a><href>[Edit]</href></a></div>

and the js code:

function my_handler (){
   alert ("button id is ..." )
}

thanks

Upvotes: 0

Views: 139

Answers (1)

James M
James M

Reputation: 16718

alert ("button id is " + this.id);

And since you've said you're already using jQuery, please separate your HTML and JS:

<div class="edit">

and

$('div.edit').on('click', function()
{
    alert ("button id is " + this.id);
});

Upvotes: 5

Related Questions