Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>depending on the actual html but you should always use <code>hasClass</code> when detecting whether or not an element has a given class</p> <p>so change the if to</p> <pre><code>if($(this).hasClass("active") </code></pre> <p>which will be true if the element has the class <code>active</code> and not only if the value of the <code>class</code> attribute is exactly the value "active"</p> <p>E.g. if you have</p> <pre><code>&lt;img class=" active image" /&gt; </code></pre> <p>then <code>hasClass("active")</code> will be true but your test will be false and even </p> <pre><code>&lt;img class=" active " /&gt; </code></pre> <p>might be treated differently in different browsers because of the whitespace around active</p> <p><strong>EDIT based on comment</strong></p> <p>You should hardly ever rely on the position of elements alone. The only scenario where you should rely on position is when the position it self is the key. E.g. when zebra colouring a table</p> <p>I suggest that you change your code to the following</p> <pre><code>$("#nav-tabs").on("click", "a", function(e) { e.preventDefault(); $(this).tab('show'); $('li#test').each(function() { var self = $(this), source = self.hasClass("active") ? "assets/img/button_home_selected3.png") : "assets/img/button_home_plain.png"; self.children(). .find(img:first).attr("src", source); }); }); </code></pre> <p>There's a few points to notice.</p> <ul> <li>When you use $(this) multiple times. Store it in a local variable instead of selecting it multiple times</li> <li>Use a more specific selector than relying only on the order the elements are found in</li> <li>Narrow the selection to only select the needed elements also when the HTML is changed. In your case adding another image as a grand child to li#test would result in the code changing the <code>src</code> for both images at the same time</li> <li>if you perform the same operation twice with different arguments instead use the if to select the input and only implement the functionality once since this makes it both easier to read the code and easier to maintain. If your case that would be select the source of the image based on whether or not the active class is present and then set the <code>src</code> accordingly</li> </ul>
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. 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