Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I'd recommend using jQuery's <code>hide</code> and <code>show</code> functions, as well as the <code>on</code> function for event handling. jQuery's main power is that it makes things "just work" across browsers, so using <code>hide</code> will let the library choose what to do in order to make that action happen, rather then you having to second guess yourself. Example:</p> <pre><code>$('#firstClicked').hide(); $('#first').on('click',function() { $('#firstClicked').show(); }); </code></pre> <p>In addition, in your original code, you have a few mistakes:</p> <pre><code>$('firstClicked').css('visiblilty','hidden'); // should be: $('#firstClicked').css('visibility','hidden'); </code></pre> <p>However, as you're worried about having to duplicate your code, you could do the following:</p> <p>HTML:</p> <pre><code>&lt;table&gt; &lt;tr&gt; &lt;td id="first" class="clickable"&gt;itemOne&lt;/td&gt; &lt;td id="firstClicked"&gt;&lt;div class="initiallyHidden"&gt;itemOneInfo&lt;/div&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td id="second" class="clickable"&gt;itemTwo&lt;/td&gt; &lt;td id="secondClicked"&gt;&lt;div class="initiallyHidden"&gt;itemTwoInfo&lt;/div&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; </code></pre> <p>JS:</p> <pre><code>$('.initiallyHidden').hide(); $('.clickable').on('click',function() { $(this).siblings("td").children(".initiallyHidden").show(); }); </code></pre> <p>This way any element with a class <code>clickable</code>, when clicked, will look for a sibling with class <code>initiallyHidden</code> and show it</p> <p>Edit: Updated example to deal with the issue raised in Christophe's comment.</p>
 

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