1

I have a problem with jquery. I created a new project and in my project you can add html tags to an article tag with jquery like 2 col or 3 col with this code:

this is my html code

<article id="1" name="1">
   Plaese Click Me!
</article>

and when I click on an article tag, I run this code

jQuery("article").click(function (e) {
    var name = $(this).attr("name");
    $(".object").attr("name", name);
    alert(name);
    e.stopPropagation();
});

and when I click on an object button, I add these html tags to article tag with this jquery code

$(".object").click(function () {
var name = parseInt($(this).attr("name"), 10);
var id = '#' + name;

var content;

    content = "<article class='col-md-6' id='" + ++name + "' name='" + name + "'>salam</article><article class='col-md-6' id='" + ++name + "' name='" + name + "'>Bye</article>";
$(id).html(content);
});

and when I click again on an article child jquery give to me parent name and I want when I click on child article tag give me child name

thanks

unicorn2
  • 844
  • 13
  • 30
Amir Nahravan
  • 69
  • 1
  • 11

1 Answers1

4

You need event delegation for dynamically added article elements:

jQuery("body").on('click','article',function (e) {
  var name = $(this).attr("name");
  $(".object").attr("name", name);
  alert(name);
  e.stopPropagation();
});
Milind Anantwar
  • 81,290
  • 25
  • 94
  • 125