0

I think this is some kind of bug. I have no idea, because I am not much of advanced on jquery.

What I am trying to do is, do a get request> value.html(result)>. Once the data is shown on the page, which are some span buttons, I made a small onclick function for testing, but it is not working.

$('#tag').click(function(){ alert(0); }); });

An get request example:

$.get( "tags.php?page=1&cat=dogs", function( data ) {
    $( ".result" ).html( data );
});

and this is what tags.php returns:

<span class="btn btn-default" role="button" id="tag">aaa</span><span class="btn btn-default" role="button" id="tag">bbb</span>

So this data is shown into the page correctly, but what I am trying to do is get the value of span. As you can see I made a simple alert function if span was clicked and it does not work. It works with other span buttons on the page, but not with this one which are displayed form get request.

Thanks for the help and sorry for bad english.

Reporter
  • 3,547
  • 5
  • 28
  • 45

2 Answers2

1

try this

$("body").on("click","#tag",function(){alert(0);});});
0

Just bind the event after you place the HTML:

$.get("tags.php?page=1&cat=dogs", function(data) {
    $(".result").html(data);
    $(".result").find('#tag').click(function() {
        alert("Hello world!");
    });
});
DoubleA
  • 1,452
  • 11
  • 23