Note that there are some explanatory texts on larger screens.

plurals
  1. POJQuery: Adding event handler to dynamically generated DOMs that triggers another handler
    primarykey
    data
    text
    <p>I have a page with a large number of DOM elements #item1 through #itemXX sharing class .itembutton, and there's a generalized .itembutton click handler that works fine.</p> <p>To the side of this is a 'search' bar that takes a query, queries sql to return a list of possible matches, then adds new DOMs to a 'search result' sidebar. This works fine, the DOMs show up.</p> <p>However, I'm trying to bind a click event to each search result such that they trigger the matching DOM in the other portion of the page. This isn't happening properly - the results appear, my debugger says each result has its own click event, it's just not doing anything. I can't tell what the click event is actually doing.</p> <p>The page is here (EDIT: removed link, doesn't have the problem anymore so not relevant) with the caveat that only the first column of items are extant in the sql backend</p> <p>Relevant code sequence here</p> <pre><code>$("#searchbar").change(function(){ if($(this).val()){ //empty check $("#searchresults").html(""); //empty result table var query = this.value; $.getJSON("search.php", {q:query}).done(function(data){ //THIS IS A 2D JSON ARRAY if (data.length == 0){ $("#searchresults").html("Nothing found! Try another search."); } else { var i = 0; while (i &lt; data.length){ var newElement = $("&lt;div class=\"searchresultbar\" id=\"result"+i+"\"&gt;&lt;img class=\"searchicon\" src="+data[i][11]+"&gt;&lt;span class=\"searchname\"&gt;"+data[i][1]+"&lt;/span&gt;&lt;/div&gt;"); $("#searchresults").append(newElement); $("#result"+i).on("click", function(data, i){ return function(data, i){ $("#"+data[i][0]).trigger("click"); }; }); /*newElement.on("click", function(idname){ return function(){ $(idname).trigger("click"); }; });*/ i++; } } }); } }); </code></pre> <p>EDIT: Not sure what was going wrong here, but as per comment I used a delegated handler and it works like a charm when I hide the appropriate dom ID in the appended div's title field</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload