Note that there are some explanatory texts on larger screens.

plurals
  1. POjQuery - refer to another class of the same object
    primarykey
    data
    text
    <p>Here's my situation:</p> <p>I've got a site that will cover 4 different subjects. I start out with 4 divs that each have the title of those subjects. For example,</p> <pre><code>&lt;div&gt;&lt;p&gt; Physics &lt;/p&gt;&lt;/div&gt; &lt;div&gt;&lt;p&gt; Chemistry &lt;/p&gt;&lt;/div&gt; &lt;div&gt;&lt;p&gt; Biology &lt;/p&gt;&lt;/div&gt; &lt;div&gt;&lt;p&gt; Math &lt;/p&gt;&lt;/div&gt; </code></pre> <p>When I click on the 'physics' div, I'd like another div to appear that elaborates on some physics problem or something. Another click causes the 'elaboration div' to disappear. I've got that working with one 'subject' here:</p> <p><a href="http://jsfiddle.net/yfrNn/2/" rel="nofollow">http://jsfiddle.net/yfrNn/2/</a></p> <p>This is the jQuery code I am using to accomplish this. </p> <pre><code>$(document).ready(function () { $(".phys").click(function () { if ($(".phys:last").css("visibility") == "visible") { $(".phys").css("visibility", ""); } else { $(".phys").css("visibility", "visible"); } }); });` </code></pre> <p>I could write this function 4 different times, once for each of the subjects I'm using, but would like a cleaner solution. I am wondering whether I can declare all of the divs in the following manner:</p> <pre><code>&lt;div class="subject phys"&gt;&lt;p&gt; Physics &lt;/p&gt;&lt;/div&gt; &lt;div class="subject chem"&gt;&lt;p&gt; Chemistry &lt;/p&gt;&lt;/div&gt; &lt;div class="subject bio"&gt;&lt;p&gt; Biology &lt;/p&gt;&lt;/div&gt; &lt;div class="subject math"&gt;&lt;p&gt; Math &lt;/p&gt;&lt;/div&gt; </code></pre> <p>And then have a certain click() function run whenever a "subject" div is clicked, have the code recognize which subject this particular div is referring to, and display the appropriate "elaboration div". </p> <p>I believe I could potentially do this with subclassing:</p> <pre><code>/* CSS */ div.subject{} .subject.phys{} .subject.chem{} .subject.bio{} .subject.math{} </code></pre> <p>Any thoughts? Is there a distinctly better way to implement something like this? </p> <p><strong>Edit</strong></p> <p>Here's an updated fiddle, with the HTML roughly as it will be in the implementation:</p> <p><a href="http://jsfiddle.net/yfrNn/6/" rel="nofollow">http://jsfiddle.net/yfrNn/6/</a></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