Note that there are some explanatory texts on larger screens.

plurals
  1. POJavascript - giving an li an active state
    primarykey
    data
    text
    <p>I'm trying to use Javascript to make a sort of slideshow. I want each li a in the slideshow navigation to be given an active state upon click and also when the slideshow cycles through to the next li a item. </p> <p>I'm relatively new to Javascript but have some very basic understanding of it. I managed to achieve part of what i wanted i.e. for the divs to cycle through and for the li navigation to be given an active state when it's clicked, but cannot work out how to give it an active CSS class when the next item in the list is active by the cycle javascript.</p> <p>I have attached the HTML and Javascript below.</p> <p>HTML</p> <pre><code>&lt;div id="tabs"&gt; &lt;div id="tab-1"&gt; &lt;h2&gt;Fantastic service, they really care.&lt;/h2&gt; &lt;div class="reviews"&gt; &lt;div class="read"&gt;&lt;a href="/reviews"&gt;Read all reviews&lt;/a&gt;&lt;/div&gt;&lt;!-- .read --&gt; &lt;/div&gt;&lt;!-- .reviews --&gt; &lt;/div&gt;&lt;!-- #tab-1 --&gt; &lt;div id="tab-2"&gt; Lorem blah blah &lt;/div&gt;&lt;!-- #tab-2 --&gt; &lt;div id="tab-3"&gt; Lorem ipsum &lt;/div&gt;&lt;!-- #tab-3 --&gt; &lt;div id="tab-4"&gt; Lorem blah blah &lt;/div&gt;&lt;!-- #tab-4 --&gt; &lt;ul class="nav"&gt; &lt;li class="tab"&gt;&lt;a href="#tab-1"&gt;Ratings &amp; Reviews&lt;/a&gt;&lt;/li&gt; &lt;li class="tab"&gt;&lt;a href="#tab-2"&gt;How it Works&lt;/a&gt;&lt;/li&gt; &lt;li class="tab"&gt;&lt;a href="#tab-3"&gt;What We Do&lt;/a&gt;&lt;/li&gt; &lt;li class="tab"&gt;&lt;a href="#tab-4"&gt;Fourth Tab&lt;/a&gt;&lt;/li&gt; &lt;/ul&gt;&lt;!-- .nav --&gt; &lt;/div&gt;&lt;!-- #tabs --&gt; </code></pre> <p><strong>Javascript</strong> </p> <pre><code>//&lt;![CDATA[ $(window).load(function(){ $(document).ready(function(){ $('#tabs div').hide(); $('#tabs div:first').show(); $('#tabs ul li:first').addClass('active'); $('#tabs ul li a').click(function(){ $('#tabs ul li').removeClass('active'); $(this).parent().addClass('active'); var currentTab = $(this).attr('href'); $('#tabs div').hide(); $(currentTab).fadeIn(400).show(); return false; }); }); var divs = $('div[id^="tab-"]').hide(), i = 0; (function cycle() { divs.eq(i).show(0) .delay(5000) .hide(0, cycle); i = ++i % divs.length; })(); });//]]&gt; </code></pre> <p>Thanks in advance.</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