Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>This doesn't solve your specific issue, but would couldn't you just do something like this:</p> <pre><code>$('#container-of-the-divs div[id^=title-]').click(function(e) { alert('Clicked div with ID: ' + e.target.id); }); </code></pre> <p>You could also just add a class to these elements and select that instead. If you're looking for performance with a ton of items, you could also add the click event onto a parent item, and then do an if statement inside which would create only one event listener, instead of N event listeners. Example:</p> <pre><code>$('#container-of-the-divs').click(function(e) { if (e.target.id.substring(0, 6) == 'title-') { alert('Clicked div with ID: ' + e.target.id); } }); </code></pre> <p>Or you could just check if $(e.target).hasClass() like mentioned before.</p> <p><strong>Updated: Here is a working example based off the code you gave:</strong></p> <pre><code>&lt;div class="result"&gt; &lt;div class="resulttext"&gt; &lt;div id="title-A0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" class="title"&gt; &lt;a href="#"&gt;A0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ&lt;/a&gt; &lt;/div&gt; &lt;div id="title-B0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" class="title"&gt; &lt;a href="#"&gt;B0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ&lt;/a&gt; &lt;/div&gt; &lt;div id="title-C0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" class="title"&gt; &lt;a href="#"&gt;C0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ&lt;/a&gt; &lt;/div&gt; &lt;div id="title-D0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" class="title"&gt; &lt;a href="#"&gt;D0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ&lt;/a&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"&gt;&lt;/script&gt; &lt;script type="text/javascript"&gt; $('div.resulttext div.title').click(function() { $i = $(this); alert('Clicked div with ID: ' + $i.attr('id')); }); &lt;/script&gt; </code></pre>
 

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