craig
craig

Reputation: 581

Applying javascript to mustache created objects

I have a mustache template in my javascript which gets put into my html

This is made up of H2 tags and ul/li tags

I have this at the bottom of my javascript

    function vara(){
        alert('f')
    }

    $('h2').click(vara)

However this does not work when I click on H2 tags created by the mustache template. How can I remedy this?

Upvotes: 0

Views: 49

Answers (2)

tymeJV
tymeJV

Reputation: 104775

Your content is being added by mustache after run-time, and since your handlers are bound at run-time, they have no idea what to bind to. The correct way is to use .on and bind the click event to the container of your appended content.

$(document).on("click", "h2", vara);

Upvotes: 1

nullpotent
nullpotent

Reputation: 9260

By waiting for a document to be ready.

Upvotes: 2

Related Questions