How to toggle some elements using jquery/javascript?

user1610952

There are multiple words like this:

word1 word2 word3 ...

Each word in an <a> tag is associated with 1 to 3 <li> tags. I need some actions to happen if the associated <a> tag is clicked.

  1. I want to make them appear if their associated word (say, word1) is clicked.
  2. If another word is clicked, the unfolded examples should disappear automatically.
  3. If word1 is clicked again, the examples should be hidden.

Here are my codes:

HTML

<a>word1</a>
<div class="sents">
    <li>This is a first example</li>
</div>

<a>word2</a>
<div class="sents">
    <li>This is a second example</li>
    <li>This is a second example</li>
</div>

<a>word3</a>
<div class="sents">
    <li>This is a third example</li>
    <li>This is a third example</li>
    <li>This is a third example</li>
</div>

Script

$(document).ready(function () {
    $('a').click(function () {
        $('a').next('div').removeClass('active');
        $(this).next('div').toggleClass('active');
    });
});

CSS

a:hover {
    background-color: yellow;
}

.sents {
    display: none;
}

.active {
    display: block;
    position: absolute;
    background-color: yellow;
    width: 100%;
    padding: 5px;
}

And this is the demo:

https://jsfiddle.net/kyubyong/umxf19vo/22/

I'm not so much comfortable with javascript/jquery. Thanks for your help in advance.

GolezTrol

Your code almost works, except for hiding the examples when the same word is clicked. To do that, you must first get the current state of the word clicked. Only make it active if it wasn't already active.

To get the current state, you can use hasClass('active'). Since you are adding the class (conditionally), you can just use addClass('active') inside an if statement.

Alternatively, you could use toggleClass('active', !isActive), but then, when deactivating a word by clicking it again, you would attempt to remove a class of which you know it already was removed before. That would be useless interaction with the DOM, and I prefer to use the if then to prevent that.

$(document).ready(function () {
    $('a').click(function () {
        var isActive = $(this).next('div').hasClass('active');
        $('a').next('div').removeClass('active');
        if (!isActive) {
          $(this).next('div').addClass('active');
        }
    });
});
a:hover {
    background-color: yellow;
}
.sents {
    display: none;
}
.active {
    display: block;
    position: absolute;
    background-color: yellow;
    width: 100%;
    padding: 5px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a>word1</a>

<div class="sents">
    <li>This is a first example</li>
</div>
<a>word2</a>

<div class="sents">
    <li>This is a second example</li>
    <li>This is a second example</li>
</div>
<a>word3</a>

<div class="sents">
    <li>This is a third example</li>
    <li>This is a third example</li>
    <li>This is a third example</li>
</div>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to toggle elements?

How to toggle elements in and out of DOM using jQuery detach() method?

How to toggle between two div elements using a button

How to toggle multiple html text elements using Vanilla JS?

How to toggle dom elements according to the states of checkboxes using jQuery?

How to toggle display style using vanilla JS on multiple elements

How to toggle between two elements using single React state

ClassList toggle works only for some elements

Toggle different elements using Vue

How to toggle Between two elements

How to do substring in some elements of string list using lambda

How to not convert some json elements to Number using Javascript

How do I check if elements exist in an array using .some()?

How to clone, modify (increment some elements) before appending using jQuery?

How can I get rid of some elements in javascript using regex?

how to remove some elements of table cells using JQuery

Iterate elements and toggle using classList in javascript

how to toggle appended elements using multiple buttons and pass info to the output JQuery

How can I toggle individual <li> elements using only basic javascript and DOM manipulation to change their styling?

How to perform some mathematical operation on some specific elements of a list using java 8?

How to find unique elements from list of tuples based on some elements using scala?

how to toggle class for multiple elements in vue js

How to toggle all .class elements in the parent container

How to trigger toggle() only once on all elements?

How to get .toggleClick to return toggle with different elements

how to toggle active class in 2 elements react

How to toggle between hidden elements in an if else statement

How to toggle to show and hide in elements in Reactjs?

How can I toggle sibling child elements?