2

I cant find why my code isn't working. When I click on the generated element the alert is not firing.

Here is a fiddle : http://jsfiddle.net/pZAdP/

And the code

<button id="addMenuItem">Add Menu Item</button>
<div id="content"></div>

function addMenuItem(){
    var span = document.createElement("span");
    span.setAttribute("id", "menu_" + inc);
    span.innerHTML = "  #menu_" + inc++ + " |";
    var content = document.getElementById("content");
    content.appendChild(span);
}

$("#addMenuItem").click(function(){
    addMenuItem();
})

$("#menu_1").on("click", function(){
    alert(this.id);
})
4
  • How is it "not working"? What does it do? What do you want it to do? Do you see any errors in your console? Have you tried to debug this yourself? Commented Jan 10, 2014 at 20:53
  • Its the onclick event that is not working when I click on the generated element Commented Jan 10, 2014 at 20:54
  • it doesnt show the alert Commented Jan 10, 2014 at 20:55
  • @Fred jQuery can't bind events to elements that don't exist yet. However, it does offer delegated binding, where the event is bound to an existing parent and acts like it was bound to the dynamic child(ren). Commented Jan 10, 2014 at 20:59

7 Answers 7

4

You need to change

$("#menu_1").on("click", function(){
    alert(this.id);
})

with :

$("#content").on("click", "#menu_1", function(){
    alert(this.id);
})

Working FIDDLE

Sign up to request clarification or add additional context in comments.

4 Comments

Well it did fix Fred's fiddle
Yup. But $("#content").on("click", "#menu_1", function(){ would be preferable since #content is closer in the DOM.
You're right, thanks for the comment I have updated it. Answering to quickly :)
Thank you! that's what I want !
2

You need to use event delegation for dynamically generated elements

You can use attribute starts with selector for all dynamically generated menu_

$("#content").on("click", "[id^='menu_']", function(){
    alert(this.id);
})

Fiddle DEMO

Comments

0

You're using jQuery, so do it the easy way, add the event handler when you create the element, that way you don't have to worry about the incrementing ID in the selector either when you create more than one element

var inc = 1;
function addMenuItem(){
    $('<span />',  {
        id   : 'menu_' + inc,
        html : '#menu_' + (inc++) + ' |',
        on   : {
            click : function() {
                alert(this.id);
            }
        }
    }).appendTo('#content');
}

$("#addMenuItem").click(function(){
    addMenuItem();
});

FIDDLE

Comments

0

http://jsfiddle.net/pZAdP/4/

Put your onclick for the menu inside the addMenuItem() function.

function addMenuItem(){
var span = document.createElement("span");
span.setAttribute("id", "menu_" + inc);
span.innerHTML = "  #menu_" + inc++ + " |";
var content = document.getElementById("content");
content.appendChild(span);

$("#menu_1").on("click", function(){
alert(this.id);})}

Remember you are selecting by Id. If you were selecting by class then putting the on click handler outside the method will work.

Comments

0

You need to use the .on with event delegation

Syntax

$(parent-selector).on(event,target-selector,callback);

Note: The parent-selector must be parent element which is present in the DOM while binding the event, generally people use document and body, but for the performance you must have the nearest parent possible to the target

Example

$(document).on("click",".button",function(){
    alert("Button Clicked");
});

Comments

0

Try this:

it will work for all dynamic span with id like menu_*:

var inc = 1;
function addMenuItem(){
    var span = document.createElement("span");
    span.setAttribute("id", "menu_" + inc);
    span.innerHTML = "  #menu_" + inc++ + " |";
    var content = document.getElementById("content");
    content.appendChild(span);
}

$("#addMenuItem").click(function(){
    addMenuItem();
})

$(document).on("click", "[id^='menu_']", function(){
    alert(this.id);
})

Comments

0

the element is not on the page when you select it, do select generated elements:

$(document).on("click","#menu_1", function(){
    alert(this.id);
})

Comments

Your Answer

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