0

My issue is that I want to set a click event dynamically. So imagine I have a button. Clicking that button shows a popup box.

In that box I have a text like this:

"Men in <span class='timelineplace-bulgaria'>Bulgaria</span>"

But the issue is that I don't have access to the button event. So I would like to set the click event with an regex like this:

$("[class*='timelineplace']").click(function () {
    var object = $(this);
    alert(object.attr("class").match(/timelineplace-(.*?)(?:\s+|$)/)[1]);
});

But yeah this only works when the popup box already existed when jQuery loading finished. So the issue is that the popup is dynamically loaded. And the click event is set when the site has finished the loadings.

So Is there a way to set a click event also at runtime to any regex matching class? Or do I really have to go to the button click event and set the onclick there.

kwoxer
  • 3,167
  • 3
  • 35
  • 51
  • Always the same question :) – Jeremy Thille Mar 06 '15 at 13:07
  • Already tried that but did not work. Now again tested and still not working. =( – kwoxer Mar 06 '15 at 13:13
  • `$("[class*='timelineplace']").on("click","select", function() { var object = $(this); alert(object.attr("class").match(/timelineplace-(.*?)(?:\s+|$)/)[1]); });` btw – kwoxer Mar 06 '15 at 13:14
  • 1
    Replace `[class*='timelineplace']` with `[class*=timelineplace]` – Jeremy Thille Mar 06 '15 at 13:14
  • Still now alert =/, also tested `$("[class*=timelineplace]").live("click", function() { var object = $(this); alert(object.attr("class").match(/timelineplace-(.*?)(?:\s+|$)/)[1]); });` – kwoxer Mar 06 '15 at 13:17
  • It seems that the regex has issue when used without `.click` – kwoxer Mar 06 '15 at 13:23
  • `$("#somebutton").on("click", function() { alert(56456); });` working but `$("[class*=timelineplace]").on("click", function() { alert(56456); });` does not and I have this one somewhere else that is also working `$("[class*='event']").click(function () { alert(56456); }` – kwoxer Mar 06 '15 at 13:24
  • Well actually it is not a regex issue. It is just the same issue I had from the very start. Because even `$(".timelineplace-plaza").on("click",function() { alert(8); });` is not working. So even without regex it does not set the onclick event correctly. – kwoxer Mar 06 '15 at 13:30
  • 1
    It's working for me : http://jsfiddle.net/vve1dLyx/ I get an alert with "Bulgaria" – Jeremy Thille Mar 06 '15 at 13:37
  • Yeah if the html and classes already exists. It works for sure. But I load that string with the class dynamically. And then I'm not able to set the onclick event =/ – kwoxer Mar 06 '15 at 13:40
  • Here you go. http://jsfiddle.net/kwoxer/vve1dLyx/2/ This is the issue I have. When the class is changed or just created at runtime. Then it does not match anymore. But it must be possible to do that somehow. There need to be a solution for that. =/ Btw it not the concrete issue. I just added that class to show the issue to you =) And could you please remove the duplicate from the title. Thank you. – kwoxer Mar 06 '15 at 13:47
  • 1
    So THAT's the question everyone asks. I have updated my fiddle : http://jsfiddle.net/vve1dLyx/3/ – Jeremy Thille Mar 06 '15 at 13:49
  • Ahh working :D Okay so putting it on the document and not the class. Thank you very much. – kwoxer Mar 06 '15 at 13:51
  • Yeah this syntax works for present and future elements. – Jeremy Thille Mar 06 '15 at 13:55

0 Answers0