0

I have following code on my HTML page.

<div id="dynBtn">
<span>Some content</span>
</div>

I am making an AJAX call that returns the following HTML content (as string):

<button id="bNew">New</button>

I am able to successfully replace the above by executing the following jQuery script from within the successful return of AJAX call:

...
//ajax call
...
        success: function(retVal)
        {
            $("div#dynBtn").replaceWith(retVal);
        } 
...

I am able to see the 'New' button added on the page. But when I click on it, it does not trigger the corresponding jQuery

$("#bNew).click()function(){
    //does not come here.
});

The js file containing this code is definitely loaded - I have confirmed this much.

Any suggestions on how can I make this new button trigger the script after it is dynamically added as above?

V_S
  • 1
  • 3
    Possible duplicate of [Event binding on dynamically created elements?](https://stackoverflow.com/questions/203198/event-binding-on-dynamically-created-elements) – CBroe Aug 21 '18 at 10:31
  • Wrap the dynamic element inside a `
    ` that is static, and listen for the `click()` event on the outer element. @CBroe has linked a very useful explanation.
    – peeebeee Aug 21 '18 at 10:56

0 Answers0