0

I am having a bit of trouble with an app i am building for an iPhone using phonegap and jQuery. i have found several threads that have told me to use on function for content created dynamically. However, i cannot get the buttons to work correctly. The follownig code creates the buttons from a loop statement;

$("#question").append('<button class="next">ClickMe</buttons>');

The following code is my click function

$(".next").on("click", function(){ alert("working"); });

So far, there are 3 buttons created, but they do not produce the alert and i get no error messages. Can anyone help explain what i have done wrong?

0

1 Answer 1

1

I think you need to bind to non-dynamic element (#question):

$("#question").on("click", ".next", function(){ alert('working') });
Sign up to request clarification or add additional context in comments.

7 Comments

i need to bing it to the dynamically created button, not the element
You should bind it to the always-existing element (#question) and supply a selector (.next) to find target element inside the always-existing element. Look carefully above and also look up the documentation (this is always helpful).
Ok, i have managed to get something out of the simulator. The second and third button produce the alert, but the first button generated doesn't do anything
I'm afraid not, as the data is fetched from a local database
You can use mock data, it's not important. Just re-create the behavior with minimal markup (HTML + JS). Maybe on the way you will find the cause of the problem yourself, and if not, more people will be able to help you when they'll have something to play with. jsfiddle.net
|

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.