0

I'm creating some elements using this code:

var itemButton = $('<button />',{'id': 'searchItemButtonId'+p[i].Id, 'class': 'itemButton', 'onclick':'javascript:alert();'}).append('Add');

The item is created with correct id and class set but the 'onclick' part doesn't work. Any ideea what is the problem here?

2 Answers 2

4

jQuery events drop the on prefix, plus you need to provide a function, not a string.

$("<button>", {
    click : function () {
        alert('something');
    }
});
Sign up to request clarification or add additional context in comments.

Comments

0

Why not use the jquery click method? So you don't need the onclick attribute

$('<button />',{'id': 'searchItemButtonId'+p[i].Id, 'class': 'itemButton'}).click( function(){ alert('asdf'); });

2 Comments

The example that happened to you "@Andy"works well, you can see here jsfiddle.net/hNdpp
Then maybe it something with my code as only the nickf way works for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.