In jQuery I know how to solve this, but in pure Javascript I don’t have any idea.
When click on chevron down my content show, but when show I want to change chevron to be chevron up.
I don’t know how to do an ‘on click’ to change the style and set the following:
transform: rotate(180deg);
.
Toggle effect, when click again to return chevron down and repeatly.
HTML:
<p>
Lorem ipsum dolor sit amet consedier tu Lorem ipsum dolor sit amet consedier tu Lorem ipsum dolor sit amet consedier tu Lorem ipsum dolor sit amet consedier tu Lorem ipsum dolor sit amet consedier tu Lorem ipsum dolor sit amet consedier tu Lorem ipsum dolor sit amet consedier tu Lorem ipsum dolor sit amet consedier tu Lorem ipsum dolor sit amet consedier tu
</p>
<div id="show-more-footer" onclick="myFunction()">
<i class="fa fa-chevron-down" aria-hidden="true"></i>
</div>
<div id="full-text">
<div class="col-sm-3">
<ul>
<li><a href="#">Seo Link</a></li>
<li><a href="#">Seo Link nummer</a></li>
<li><a href="#">Number 3 Seo Link</a></li>
<li><a href="#">Seo</a></li>
</ul>
</div>
<div class="col-sm-3">
<ul>
<li><a href="#">Seo Link</a></li>
<li><a href="#">Seo Link nummer</a></li>
<li><a href="#">Number 3 Seo Link</a></li>
<li><a href="#">Seo</a></li>
<li><a href="#">Number 5</a></li>
</ul>
</div>
<div class="col-sm-3">
<ul>
<li><a href="#">Seo Link</a></li>
<li><a href="#">Seo Link nummer</a></li>
<li><a href="#">Number 3 Seo Link</a></li>
<li><a href="#">Seo</a></li>
</ul>
</div>
</div>
JS:
function myFunction() {
var x = document.getElementById('full-text');
if (x.style.display === 'block') {
x.style.display = 'none';
} else {
x.style.display = 'block';
}
}
CSS:
#show-more-footer{
cursor:pointer;
}
#full-text{
display:none;
}
2
Answers
You can directly change style of an element in JavaScript:
This will appear in the HTML as :
You could also use the classList API and JS :
And the JS :
MDN – element.classList
CanIUse – classList
The fact that some browsers don’t support the
.toggle()
method is not a problem, use a custom function which uses.add()
or.remove()
.Last advices (optional) :
I would use CSS transitions with this kind of features (like
transition: all 0.3s ease-in-out;
) and change your hide/show method by setting aheight:0
andoverflow:hidden
to the content you hide, andonclick
of the chevron you give himelement.style.height: auto
.my first advice would be to not use inline javascript to call your function, you should use a listener instead
then you can use the callback first argument to get your event