Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums JavaScript Attach click event on replaced text

  • This topic is empty.
Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #247587
    rolandas
    Participant

    How can I attach click event on a created element? I have to replace for example p tag’s text with a button and then do something when I click on that button.
    Example: https://jsbin.com/fakepi/1/edit?html,js,output

    #247611
    Shikkediel
    Participant

    Use a delegated event…

    $(document).on('click', 'button', function() { ... })
    
    #247615
    Shikkediel
    Participant

    That is, if you want to create the code for the event beforehand. If you attach a handler after the button’s been created, it’ll also respond.

    #247647
    rolandas
    Participant

    Nice, thank you very much :)

Viewing 4 posts - 1 through 4 (of 4 total)
  • The forum ‘JavaScript’ is closed to new topics and replies.