Note that there are some explanatory texts on larger screens.

plurals
  1. POHow does one to get the #id of an element from a clicked link using jQuery?
    primarykey
    data
    text
    <p>New to jQuery/javascript here and looking for a little help. I am writing a site with a simple navigation menu. It works like this. A user clicks a link in the menu, the link is set to an 'active' colour and the appropriate section is set from hidden to visible ( all of the sections are set to hidden when the document loads ). When the user clicks another link that link is set to the active color, the others are set to the default colour, any visible sections are set to hidden and the relevant section is set to visible. This works just fine, but I used individual element and its very ugly with a lot of repetition. I am in the process of rewriting the code to use .classes and be a little bit more flexible with less repeated code.</p> <p>The Question I have is this: How do I tell it what section to show?</p> <p>User clicks on a.linkfornavigation, colour is set, section.blah are set to hidden, section#thisiswhatyouwant is set to visible. But how do I tell a.linkfornavigation where it should point? each section has a unique #id ( I guess so could the link they click ) but how do I get the #id of the relevant linked section from the clicked link?</p> <p>Any help at all would be very much appreciated.</p> <p>HTML:</p> <pre><code>&lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;link rel="stylesheet" href="css/linkthinger-hybrid.css" /&gt; &lt;script src="js/jquery.js" type="text/javascript"&gt;&lt;/script&gt; &lt;script src="js/linkthinger-hybrid.js" type="text/javascript"&gt;&lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;section id="links"&gt; &lt;a class="thinger" href="#"&gt;Thing One&lt;/a&gt; &lt;a class="thinger" href="#"&gt;Thing Two&lt;/a&gt; &lt;a class="thinger" href="#"&gt;Thing Three&lt;/a&gt; &lt;a class="thinger" href="#"&gt;Thing Four&lt;/a&gt; &lt;a class="thinger" href="#"&gt;Thing Five&lt;/a&gt; &lt;/section&gt; &lt;section id="things"&gt; &lt;section id="thingone" class="thing"&gt;I am thing one.&lt;/section&gt; &lt;section id="thingtwo" class="thing"&gt;I am thing two.&lt;/section&gt; &lt;section id="thingthree" class="thing"&gt;I am thing three.&lt;/section&gt; &lt;section id="thingfour" class="thing"&gt;I am thing four.&lt;/section&gt; &lt;section id="thingfive" class="thing"&gt;I am thing five.&lt;/section&gt; &lt;/section&gt; &lt;/body&gt; </code></pre> <p>javascript:</p> <pre><code> $(document).ready(function(){ // Hides .thing class as soon as the DOM is ready. // $('section.thing').hide(0); // Highlight selected link &amp; set all others to default. $('#links a').click(function(){ $(this).addClass('selected'); $(this).siblings().removeClass('selected'); }); // Shows one section and hides all others on clicking the noted link. $('a.thinger').click(function() { $('section.thing').hide(0); $('#thingone').fadeIn('slow'); return false; }); }) </code></pre> <p>CSS:</p> <pre><code>a { color: darkgreen; } .selected { color: red; } section.thing { background-color: blue; } </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.
 

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