0

As title above. My click event is not hitting when i click my button. I made my button via JS Loop. My click event function is in outside of my loop function. As I notice when i am debugging, when i try to put it inside the function of the loop the click function it's hitting properly now.

Is there any reason why?

for (var i = 0; i < dataLength; i++) {
    var docsLength = data.data[i].resultData;
    var title = data.data[i].resultData[i].Type.Description;
    var card = '<div class="card" id="HelloWorld_' + i + '"> <div class="card-header bg-success"><h3 class="card-title">' + title + ' </h3 ></div> <div class="card-body"> </div>'
    $("#card_documents > .card-body").append(card);

    for (var b = 0; b < docsLength.length; b++) {
        var id = "HelloWorld_" + i;
        var row = '<div class="row mt-2" style="font-size:20px">'
            + '<div class="col-md-1"><input type="checkbox" style="height:20px; -webkit-box-shadow:none" class="form-control flat mt-2"> </div >'
            + '<div class="col-md-1"> <input type="button" id="btnClickHello' + b + "" + i + '" value="Upload"></div>'
            + '<div class="col-md-10" style="color:green"> ' + data.data[i].resultData[b].Docs + ' </div >'
            + '</div >';
        $("#" + id + " > .card-body").append(row);
    }
}



$("#btnClickHello21").click(function (e) {
    e.preventDefault();
    alert("a");
});
5
  • You need to wrap your click event function into a document.ready function. Commented Sep 17, 2018 at 6:48
  • @Martin Not essentially.. Commented Sep 17, 2018 at 6:50
  • @KiRa Why have you used the number '21' specifically for the click event? Is there any reason? Commented Sep 17, 2018 at 6:53
  • @JonesJoseph i will edit it soon. Its just my testing for now if it is working or not. Commented Sep 17, 2018 at 6:53
  • @Martin my whole code is inside doc.ready Commented Sep 17, 2018 at 6:54

4 Answers 4

1

For dynamically added elements, you need to use jQuery.on

$(".card-body").on("click", "#btnClickHello21", function (e) {
    e.preventDefault();
    alert("a");
});
Sign up to request clarification or add additional context in comments.

3 Comments

like you said when element is dynamically added I should use on right?. Like on my problem?
@KiRa - Yes and make sure you bind the event the available parent element at the moment.
@KiRa - Glad to help you :)
1

Another way of doing it (despite the 2 answers already), is by wrapping your click event function into a document.ready function.

$( document ).ready(function() {
    $("#btnClickHello21").click(function (e) {
        e.preventDefault();
        alert("a");
    });
});

jQuery document.ready docs: http://learn.jquery.com/using-jquery-core/document-ready/

3 Comments

And that doesn't work? I don't see the document.ready block in your question, so that's why I suggested it.
in my post i didn't include it. sorry
No problem, just wanted to point that out, despite the already 2 working answers that you received. Always good to have multiple angles to broaden ones knowledge. :)
1

You are adding element dynamically, that is why click event is not working beacuse all click events get bind to DOM on load. To enable click on dynamically added element you can use below code -

$('body').on('click', '#btnClickHello21', function(e) {
  e.preventDefault();
  alert("a");
});

This will work for all a tags with #btnClickHello21 in the body, whether already present or dynamically added later.

Comments

-1

as per my knowledge it is not hitting because you are not wrapping your function within a document.ready(). DOM and function is loaded before you create a button.

you need a event delegation concept like.

$(document).ready(function(){
  $("#btnClickHello21").on('click',function(){
            //do your stuff.
    });

})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.