最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Click function not working on div class created on the fly - Stack Overflow

programmeradmin5浏览0评论

I am unsure as to how to approach this problem.

I have some buttons made in the HTML with some data attributes added to them. These buttons have a class called roleBtn which will call my jQuery roleBtnClicked function and grab the variables in the HTML's data attributes.

$(".roleBtn").click(roleBtnClicked);

function roleBtnClicked(event){
    reg.roleName  = $(this).html();           /* Get role name: Actor */
    reg.blueBtn   = $(this).data('blue');     /* Get blue-btn-1 */
    reg.the_Num   = $(this).data('num');      /* Get the number */
    reg.modal_ID  = $(this).data('modal');    /* Get modal-1 */

Now using this information, after the roleBtn is clicked a modal window will e up, I then have a doneButton which will close the modal window and use the variables from the data attributes to then generate new HTML on the fly. This new HTML will contain a button with the class of blueBtn.

My problem is that my click function for blueBtn won't work on a blue button that was created on the fly. It will work on a div that already has the class blueBtn before hand, but doesn't work if it was created on the fly.

Do you know a workaround to this? Or am I missing something simple?

After the doneButton is clicked I have another function that creates the new HTML including the blueBtns on the fly:

$('.selected_row .choices-col-left').append('<div class="blueBtn-holder" id="blueBtnHolder-'+theNum+'"><div class="blueBtn" id="'+blueBtn+'" row="'+rowName+'" modal="'+modal_ID+'" num="'+theNum+'">'+roleName+'</div></div>');

My blue button click function which doesn't work

$(".blueBtn").click(blueBtnClicked);

function blueBtnClicked(event){
    alert("Where are you blueBtn on the fly?");
    console.log("Where are you blueBtn on the fly?");
};

I am unsure as to how to approach this problem.

I have some buttons made in the HTML with some data attributes added to them. These buttons have a class called roleBtn which will call my jQuery roleBtnClicked function and grab the variables in the HTML's data attributes.

$(".roleBtn").click(roleBtnClicked);

function roleBtnClicked(event){
    reg.roleName  = $(this).html();           /* Get role name: Actor */
    reg.blueBtn   = $(this).data('blue');     /* Get blue-btn-1 */
    reg.the_Num   = $(this).data('num');      /* Get the number */
    reg.modal_ID  = $(this).data('modal');    /* Get modal-1 */

Now using this information, after the roleBtn is clicked a modal window will e up, I then have a doneButton which will close the modal window and use the variables from the data attributes to then generate new HTML on the fly. This new HTML will contain a button with the class of blueBtn.

My problem is that my click function for blueBtn won't work on a blue button that was created on the fly. It will work on a div that already has the class blueBtn before hand, but doesn't work if it was created on the fly.

Do you know a workaround to this? Or am I missing something simple?

After the doneButton is clicked I have another function that creates the new HTML including the blueBtns on the fly:

$('.selected_row .choices-col-left').append('<div class="blueBtn-holder" id="blueBtnHolder-'+theNum+'"><div class="blueBtn" id="'+blueBtn+'" row="'+rowName+'" modal="'+modal_ID+'" num="'+theNum+'">'+roleName+'</div></div>');

My blue button click function which doesn't work

$(".blueBtn").click(blueBtnClicked);

function blueBtnClicked(event){
    alert("Where are you blueBtn on the fly?");
    console.log("Where are you blueBtn on the fly?");
};
Share Improve this question edited Jul 24, 2019 at 18:10 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked May 3, 2013 at 15:21 Leon GabanLeon Gaban 39.1k122 gold badges349 silver badges550 bronze badges 4
  • google.fr/… – A. Wolff Commented May 3, 2013 at 15:23
  • Dynamic elements will need the delegated version of on(), a search would have given you a gazillion answers to this question ? – adeneo Commented May 3, 2013 at 15:23
  • 1 (O.T.) When you enter the [jQuery] tag asking a Q there should be a popup saying: "Hey you! Did you knew about the cool .on() method?" :) we really miss that feature! – Roko C. Buljan Commented May 3, 2013 at 15:25
  • 1 possible duplicate of stackoverflow./questions/11288516/… and other 100 Q – Roko C. Buljan Commented May 3, 2013 at 15:27
Add a ment  | 

2 Answers 2

Reset to default 7

Try this:- You need Event delegation for dynamically created elements using .on()

$(".selected_row").on('click', '.blueBtn', blueBtnClicked);

function blueBtnClicked(event){
    alert("Where are you blueBtn on the fly?");
    console.log("Where are you blueBtn on the fly?");
};

Demo

When you just bind a click event, it will be only bound to the existing DOM elements, so you want to bind the event to the parent element and later any elements you add with the same selector with that container will have the event available by delegation.

From Jquery Docs

Delegated events have the advantage that they can process events from descendant elements that are added to the document at a later time. By picking an element that is guaranteed to be present at the time the delegated event handler is attached, you can use delegated events to avoid the need to frequently attach and remove event handlers. This element could be the container element of a view in a Model-View-Controller design, for example, or document if the event handler wants to monitor all bubbling events in the document. The document element is available in the head of the document before loading any other HTML, so it is safe to attach events there without waiting for the document to be ready.

That's because you have to reassign event handlers for newly created items.

A better approach would be using .on on the container object, and then specify the child objects that should respond to the clicks:

$(".container").on('click', '.blueBtn', blueBtnClicked);

This way even if you ad objects on the fly, they will still respond. This is actually a lot more efficient way of handling that, because you only create one event handler as oppose to many. This is actually called event delegation.

Remember that when you do $(selector).click, you're telling jQuery to find all elements matching selector and assign the specific "click handler" to them. This does not happen again when you create new objects, because you're not telling jQuery to handle every future object as well (jQuery will not be aware of you adding a new button and will not assign a handler to it).

发布评论

评论列表(0)

  1. 暂无评论