Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>if you wanted to actually target the current <code>&lt;a&gt;</code> generically out of many <code>&lt;a&gt;</code>'s when selecting an anchor, use the <code>event</code> that's passed through on <code>.on('click</code>)`:</p> <pre><code>&lt;a data-example="test-london"&gt;London&lt;/a&gt; &lt;a data-example="test-rome"&gt;Rome&lt;/a&gt; &lt;a data-example="test-paris"&gt;Paris&lt;/a&gt; &lt;script type="text/javascript"&gt; $(document).on('click', 'a', function( event ) { alert( $(event.target).attr( 'data-example' )); }); &lt;/script&gt; </code></pre> <p><strong>Fiffle</strong>: <strong><a href="http://jsfiddle.net/hM4kC/" rel="nofollow noreferrer">http://jsfiddle.net/hM4kC/</a></strong></p> <p>Although, I wouldn't suggest this at all normally. As this just an example on how you would pick the <em>selected</em> <code>&lt;a&gt;</code> anchor out of an jQuery object which is retrived from many results (for example, the current page would collect 138 Nodes! Which is an <em>expensive</em> operation, see below:)</p> <p><img src="https://i.stack.imgur.com/bY6kr.jpg" alt="enter image description here"></p> <p>It's much quicker, cleaner &amp; semantic to stick to selecting by classes or ID, so in your case:</p> <pre><code>&lt;a data-bookmark-item="2" data-userid="501" class="getbook"&gt;John Grisham&lt;/a&gt; &lt;a data-bookmark-item="3" data-userid="551" class="getbook"&gt;J.K Rowling&lt;/a&gt; &lt;a data-bookmark-item="4" data-userid="201" class="getbook"&gt;JRR Tolkien&lt;/a&gt; &lt;script type="text/javascript"&gt; $(document).on('click', '.getbook', function() { var bookMark = $(this).data('bookmark-item'), userid = $(this).data('userid'); console.log( bookMark + ' for user: '+ userId ); somefunction( bookMark, userId ); //Pass them through to where needed. }); &lt;/script&gt; </code></pre> <p>Where as you'd replace <code>bookmark-item="4"</code> with <code>bookmark-item="&lt;?=$item_id;?&gt;"</code> and utilising jQuery's <a href="http://api.jquery.com/jQuery.data/" rel="nofollow noreferrer"><code>.data()</code></a> to collect relevant DOM information.</p> <p><strong>Fiddle:</strong> <a href="http://jsfiddle.net/N99B6/3/" rel="nofollow noreferrer">http://jsfiddle.net/N99B6/3/</a></p>
    singulars
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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