0

i am having some problems here. Here is my code:

                 $(document).ready(function() {
                     $("#button").click(function(){
                         alert("hello");
                         var a = $("textarea").val();
                         var b = $("#search").val();
                         var c = $("textarea").length;
                         var d = $("#search").length;
                         var array = [];
                         var st = "";
                         for(var e=0;e<c;e++)
                         {
                            for (var f=0;f<d;f++)
                            {
                               if(a[e+f] === b[f])
                               {
                                    alert(a[e+f]);
                                    array.push(a[e+f]);
                                    if (array.length === d)
                                    {
                                    for(var g in array)
                                       {
                                        st += array[g];
                                       }
                                        a.substr(e,e+f).replaceWith("vinh");
                                        break;                                      
                                    }                                   
                               }
                               else
                               {
                               break;
                               }
                            }

                         }
                     });
                });

The bad news is that when the document is ready, there isn't any $("#button") at first. Only after i trigger a code that append button to html but it is too late, the button won't run. Can anyone solve this ?. Many thanks to your help

4 Answers4

2

The best way to trigger click on dynamically created element is to listen to the click event in DOM then fire it on your desire element then it will check for the target where is click been fired.

Use

$(document).on("click","#button",function(){ // code goes here })

Learn more about .on visit the official jquery documentation Click Here

M.Tanzil
  • 1,987
  • 1
  • 12
  • 28
1
$(document).ready(function() {
$(document).on("click","#button" function(){
    alert("hello");
    var a = $("textarea").val(),
        b = $("#search").val(),
        c = $("textarea").length,
        d = $("#search").length,
        array = [],
        st = "";

    for(var e=0;e<c;e++){
        for (var f=0;f<d;f++){
            if(a[e+f] === b[f]){
                alert(a[e+f]);
                array.push(a[e+f]);
                if (array.length === d){
                    for(var g in array){
                    st += array[g];
                    }
                    a.substr(e,e+f).replaceWith("vinh");
                    break;                                      
                }                                   
            }else{
                break;
            }
        }
    }                                       
});
});
Samir
  • 1,312
  • 1
  • 10
  • 16
0

You have to use the buttons parent for this. This is an example using "body" as parent:

$("body").on("click", "#button", function(){
    // Add your code here
}

Take a look at this thread for more info: Event binding on dynamically created elements?

Community
  • 1
  • 1
Kamelkent
  • 329
  • 7
  • 16
0

You can write the jQuery code to be run on the button element in a separate .js file and load it asynchronously after the code that loads this button using $.getScript().

Raunaq Sachdev
  • 518
  • 4
  • 7