Note that there are some explanatory texts on larger screens.

plurals
  1. POjquery, get id/class/other from clicked item and reuse
    primarykey
    data
    text
    <p>I have this:</p> <pre><code>jQuery(document).ready(function(){ jQuery('a.color2030').click( function() { jQuery('tbody').hide(); jQuery('tbody.color2030').show(); }); jQuery('a.color2031').click( function() { jQuery('tbody').hide(); jQuery('tbody.color2031').show(); }); ... ... jQuery('a.color2037').click( function() { jQuery('tbody').hide(); jQuery('tbody.color2032').show(); }); }); </code></pre> <p>But would like not to have to repeat the block of code for each link. I can get the id from the link in any way. It could be color2030 etc.</p> <p>I have tried:</p> <pre><code>&lt;a href="#color2030" class="color2030" id="test2030"&gt; &lt;a href="#color2031" class="color2031" id="test2031"&gt; &lt;a href="#color2032" class="color2032" id="test2032"&gt; </code></pre> <p>And in jQuery:</p> <pre><code>jQuery('a').click(function() { // this works var id = 'test'; alert(id); // will alert 'test' // this does not work var id = $(this).attr('id'); //update2: should have been jQuery(this... alert(id); // no alert but error on page }); </code></pre> <p>But I cannot seem to get hold on the id. The "var id" line seems to have an error or so. Is there another way to get read an id/class or other tag from an a-tag when pressed?</p> <p>BR. Anders</p> <p>UPDATED: due to a typo that two persons spotted in not much more than 30 sek!</p> <p>UPDATE2 and solved! The error I could not spot was the $(this) where it should have been jQuery(this) in the 'var id' line</p> <p>Final update with solution This is was is doing the trick</p> <pre><code>jQuery(document).ready(function(){ jQuery('a').click( function() { var id = this.id; alert(id); jQuery('tbody').hide(); jQuery('tbody.'+id).show(); }); }); </code></pre>
    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. 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