user3000457
user3000457

Reputation:

onclick event for a dynamic element

I have a dynamic button which gets added by html attribute:

EDITED:

$("#gallery").html("<button id='" +img_name.substring(img_name.indexOf("uploaded_images")) 
    + "' onclick='testrf('test')' 
    class='btn btn-danger btn-xs delete_btn' >Delete</button>");

I have my function as:

function testrf(param)
{
    alert(param);
};

but it gives me testrf not defined, is that because my element is added dymanicaly?

Am I missing something?

Upvotes: 0

Views: 95

Answers (3)

Hunter
Hunter

Reputation: 146

There's an entire practice that says it's a bad idea to have inline functions/styles.

Just use jQuery.

JS

$('button').click(function(){
  alert(this.id);})

HTML

 <button id='1234' class='btn btn-danger btn-xs delete_btn' >Delete</button>

jsFiddle: http://jsfiddle.net/5kfYQ/1/

Upvotes: 1

form3
form3

Reputation: 127

onclick="testrf('test')"

You should use different quotes

Upvotes: 0

morantis
morantis

Reputation: 106

It is not coming out as a function. It looks right here, but there may be a missing semicolon right before the function is defined. It is looking for testrf rather than defining it as a function. Syntax check your entire page, not just the function.

Upvotes: 0

Related Questions