Note that there are some explanatory texts on larger screens.

plurals
  1. POjQuery Event-Handler changes CSS-Class with a shift of one click
    primarykey
    data
    text
    <p>I have some kind of strange behaviour using jQuery's Click-Function. I also tried it with the on-EventHandler. There is some simple markup from Twitter's Bootstrap Navbar and I'm trying to change the CSS-Class of an li-tag. In this case I want to remove the CSS-Class active and add it to the currently clicked li-tag. But the change get's applied with a shift of one click. So when when e.g. 'Preview' is the current visual active element and I click on 'Document', the CSS-Class seems not to be changed. When I click afterwards on 'Slidecast', then the visual active element is 'Document' which is not the expected behavoiur. Next Click on 'Preview' and now 'Slidecast' is the visual active element. What I'm doing wrong? I tried it with remove and add and also with toggleClass, but that doesen't matter. Here is my code and markup. JS is wrapped into a Dom-Ready-Function.</p> <pre><code>&lt;div class="navbar"&gt; &lt;div class="navbar-inner"&gt; &lt;ul class="nav"&gt; &lt;li class="active"&gt;&lt;a href="#!document_defaultview"&gt;Document&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="#!document_slidecast"&gt;Slidecast&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="#!document_preview"&gt;Preview&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="#!document_notes"&gt;Notes&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="#!document_questions"&gt;Questions&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="#!document_activities"&gt;Activities&lt;/a&gt;&lt;/li&gt; &lt;/ul&gt; &lt;/div&gt; &lt;/div&gt; </code></pre> <p>JS</p> <pre><code>$("#document_navbar a").on('click', function(e) { // Hide all content, display the one related to hash-tag $(".document_view").toggle(false); $(app_locationToId($(this).attr("href"))).toggle(true); // Here is the part that does not work: //$("#document_navbar li.active").removeClass("active"); //$("a[href='" + window.location.hash + "']").parent().addClass('active'); // Another try ... same result as described above! $("#document_navbar li.active").toggleClass("active", false); $("a[href='" + window.location.hash + "']").parent().toggleClass("active", true); }); </code></pre> <p>OK according to your answers, here is the code I've used to handle that problem: </p> <pre><code>$("#document_navbar a").on('click', function(e) { new_location = app_locationToId($(this).attr("href")); // Hide all content, display the one related to hash-tag $(".document_view").toggle(false); $(new_location).toggle(true); // Change visual active li-tag $("#document_navbar li.active").toggleClass("active", false); $("a[href='" + app_IdToLocation(new_location) + "']").parent().toggleClass("active", true); }); </code></pre>
    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