Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Click event is not working for dynamically added button

I reffered Creating a div element in jQuery and creatd a div element using javascript. However when I added a button element dynamically, click is not working. What change do we need to do to make the button click working?

Note: We cannot move the function outside of document.ready due to kendo control requirements mentioned in Binding to multiple view models nested in the Dom

Updated References

  1. Wiring up click event using jQuery on() doesn't fire on injected HTML via Ajax call
  2. how to attach jquery event handlers for newly injected html?
  3. After injecting html by jquery, the event handlers doesn't work with/without delegate

CODE

<head>

    <title>Test</title>
    <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
    <script src="http://cdn.kendostatic.com/2013.2.716/js/kendo.all.min.js"></script>



<script type="text/javascript">

    //lijo
    $(document).ready(function () 
    {

    $(".statiscDIV").append('<div>FIRST</div>');
      $(".statiscDIV").append('<div>hello <button class="MakeHoldDetailLinkButton" onclick = "showMakeAndHold();">View</button>  </div>');


    //lijo
    function showMakeAndHold() 
    {

        alert("HIIIIIII");

    }


    });

 </script>

</head>

<body>

 <div class="statiscDIV">

A

 </div>

</body>
like image 835
LCJ Avatar asked Jun 07 '14 17:06

LCJ


People also ask

How to add event dynamically in js?

Attaching the event dynamicallyclassName = 'dynamic-link'; // Class name li. innerHTML = dynamicValue; // Text inside $('#links'). appendChild(li); // Append it li. onclick = dynamicEvent; // Attach the event!

How can create button click event dynamically in asp net?

public form1() { foreach (Panel pl in Container. Components) { pl. Click += Panel_Click; } } private void Panel_Click(object sender, EventArgs e) { // Process the panel clicks here int index = Panels. FindIndex(a => a == sender); ... }


1 Answers

When you inject code into the DOM, the jQuery event handlers are not attached/bound to the new elements. (jQuery already did the bindings to DOM elements before the new code was injected). Therefore, when you click a button, no jQuery click event is trapped.

To attach event handlers (and thereby grab events from) injected elements, you must use jQuery .on(), as follows:

jsFiddle Demo

$(".statiscDIV").append('<div>FIRST</div>');
$(".statiscDIV").append('<div>hello <button class="MakeHoldDetailLinkButton">View</button>  </div>');

$(document).on('click','.MakeHoldDetailLinkButton',function(){
    showMakeAndHold();    
});

function showMakeAndHold() {

    alert("HIIIIIII");

}

The .on() method was added in jQuery 1.7 to replace bind(), .delegate(), and .live() -- it does the same thing as all of these. (To unbind event handlers from ANY DOM elements, use .off())

Source: http://api.jquery.com/on/

like image 167
cssyphus Avatar answered Oct 16 '22 16:10

cssyphus