2

I have created a dynamic page using jquery mobile, how to get an event after creating this page? Also i need an alert from this button.

this is my code:

 $("#test").on('click',function(){
    var newPage = $("<div data-role=page data-url=testpage><div data-role=header>        <h1>TEST</h1></div><div data-role=content> <input type='button' value='clic kMe' id='click_me'> </div></div>");
    newPage.appendTo( $.mobile.pageContainer );
    $.mobile.changePage( newPage );
 });

1 Answer 1

3

Use this:

    $(document).on('click','#click_me',function() {
        alert('Click');
    });  

or in case of your code:

$("#test").on('click',function(){
    var newPage = $("<div data-role=page data-url=testpage><div data-role=header><h1>TEST</h1></div><div data-role=content> <input type='button' value='clic kMe' id='click_me'> </div></div>");
    $(document).on('click','#click_me',function() {
        alert('Click');
    }); 
    newPage.appendTo( $.mobile.pageContainer );
    $.mobile.changePage( newPage );
});
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks Gajotres, click event is firing now, i need an event after create this page,
This kind of binding will work always no matter if element exists in DOM or not. Do you maybe need anything else?
yea!, i want to append some data to this page, so how i get pageshow event form this page?
You will need to generate an id for that page,lets say that id is page1, then your code would work like this: $(document).on('pageshow ', '#page1', function(){ }); Because this is delegated binding you can do it after the click bind I showed you in my answer or in any other place. But remember page id is important, it is used so correct page event can be bound to a correct page.

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.