JavaScript Jquery on click event not working jquery click event not firing,jquery click event listener,jquery

You need to change

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

with :

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

Working FIDDLE

Answer:1

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

Answer:2

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

Answer:3

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.

Answer:4

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");
});
Answer:5
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);
    $("#menu_1").on("click", function(){
    alert(this.id);
})
}

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

You are attempting to add an event to a element which does not yet exist.

Answer:6

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);
})
Answer:7

I have a table and in one td I have an another table. What I am trying to do is select only the last tr from the first table (which is represented in the example below). My table HTML: <table&...

I have a table and in one td I have an another table. What I am trying to do is select only the last tr from the first table (which is represented in the example below). My table HTML: <table&...

  1. jquery select last element
  2. jquery select last child
  3. jquery select last option
  4. jquery select last row of table
  5. jquery select last of type
  6. jquery select last li in ul
  7. jquery select last element with class
  8. jquery select last option in dropdown
  9. jquery select last child of parent
  10. jquery select last element in array
  11. jquery select last two elements
  12. jquery select last child with class
  13. jquery select last div
  14. jquery select last of class
  15. jquery select last character of string
  16. jquery select last item
  17. jquery select last tr
  18. jquery select last td in tr

I have 2 jstrees and now I am trying to implement a drag and drop option between them, but I can't seem to manage to obtain the id's of the element i am dragging or the new parent's id (after drag and ...

I have 2 jstrees and now I am trying to implement a drag and drop option between them, but I can't seem to manage to obtain the id's of the element i am dragging or the new parent's id (after drag and ...

Here is my code so far: dojoConfig = {parseOnLoad: true}; resetStopwatch(); require(["dojo/query", "dijit/form/ToggleButton", "dijit/form/Button", "dojo/dom", "dojo/dom-attr", "dojo/...

Here is my code so far: dojoConfig = {parseOnLoad: true}; resetStopwatch(); require(["dojo/query", "dijit/form/ToggleButton", "dijit/form/Button", "dojo/dom", "dojo/dom-attr", "dojo/...

  1. change button text javascript
  2. change button text on click
  3. change button text jquery
  4. change button text color
  5. change button text swift
  6. change button text unity
  7. change button text css
  8. change button text color swift
  9. change button text color css
  10. change button text tkinter
  11. change button text excel
  12. change button text onclick react
  13. change button text android studio
  14. change button text on hover
  15. change button text vba
  16. change button text size
  17. change button text color bootstrap
  18. change button text java
  19. change button text color react native
  20. change button text squarespace

This is not something I'm actually going to use, I know I should not have two headers on a page, this is just an example of my problem. This works perfectly when there is only one on the page, but ...

This is not something I'm actually going to use, I know I should not have two headers on a page, this is just an example of my problem. This works perfectly when there is only one on the page, but ...

  1. moving elements around javascript
  2. jquery moving elements around