0

I am an absolute JS noob and need a little help. I am using bootstrap 3.1.1

<a class="" data-toggle="collapse" data-parent=".event-list-links" href="#collapse-<?php the_ID(); ?>">
 <i class="icon-chevron-sign-down"></i>
 </a>

Now on clicking the link the .icon-chevron-sign-down should change to .icon-chevron-sign-up how do I do that?

Thanks in advance!

4 Answers 4

1

Another try:

<i class="icon-chevron-sign-down" onclick="changeClass(this)"></i>

And the script:

function changeClass(e) {
  e.className = 'icon-chevron-sign-up';
}

And back to first try:

<i class="icon-chevron-sign-down" onclick="(function (e) { e.className = 'icon-chevron-sign-up'; })(this)">halo</i>

EDIT

To toggle between the two classes:

function changeClass(e) {
  if (e.className === 'icon-chevron-sign-up') {
    e.className = 'icon-chevron-sign-down';
  } else {
    e.className = 'icon-chevron-sign-up';
  }
}
Sign up to request clarification or add additional context in comments.

6 Comments

Your second solution worked. But now I need the opposite behaviour too. That when my container collapses again, it switches back...
You mean when you click again it should go back to icon-chevron-sign-down? It added an edit to my answer.
Hi phylax, I tried but still don't get it :) - When I use the <i class="icon-chevron-sign-down" onclick="changeClass(this)"></i> together with your edited script in the pagefooter, nothing happens. The only thing working is you middle-line. What is the fitting html to yor function?
I created a plunker to demonstrate it: plnkr.co/edit/9Q1fZsMLcLhslMULUOJv?p=preview
1. it works so far. 2. with a strange behaviour. For the first to clicks everything is ok. Then it skips on click, and continuing, leading the function to do it reverse afterwards. Maybe you have a look yourself on [link]my-home-is-my-kassel.de/#collapse-1612706[link] it is about the up / down sign below the calendar. Thanks!
|
0

You can do it pretty easily with jQuery. You'll want to add a class to your tag so you can select it:

<a class="toggle-link" data-toggle="collapse" data-parent=".event-list-links" href="#collapse-<?php the_ID(); ?>">
    <i class="icon-chevron-sign-down"></i>
</a>

Then just add a click handler that removes the icon-chevron-sign-down class and adds a icon-chevron-sign-up class.

$'.toggle-link').click(function() {
    $(this).find('.icon-chevron-sign-down').
        removeClass('icon-chevron-sign-down').
        addClass('icon-chevron-sign-up');
    return false;
});

I'd note that this won't switch back to icon-chevron-sign-down if you click again. That code is a little more complicated. Let me know if that's what you're looking for and I'll update this answer.

5 Comments

-sign-down is not a class
I was avoiding having to type "icon-chevron-sign-down" over and over. I'll update my answer if that's confusing.
you've missed leading dotes in selectors.
Oops. Need more sleep. Updated
It sould switch in both direction, since it is a collapsing container.
0

Use the setattribute

Var elem = document.getElementsByTagName('a');
Firstelem=elem[0];
Firstelem.setAttribute("class","your clAss here");

Comments

0

Just replace your class (IE8+) in script tag (place it jest before closing body tag):

document.onload(function() {
    var element = document.querySelectorAll('i.icon-chevron-sign-down');
    element.onclick(function(e) {
        element.className.replace('icon-chevron-sign-down'
                                , 'icon-chevron-sign-up');
    });

});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.