0

I have to functions: openNav and closeNav and a button:

 <a href="#" data-toggle="sidebar-collapse-button">
                    <img class="sidebar-img" id = "collapse-img" src="${pageContext.request.contextPath}/imgs/main/sidebar/collapse.png"/>
                    <span class="sidebar-text">Collapse</span>
 </a>

A try to set a conditional onClick like this:

var collapsed = 0;

$('[data-toggle=sidebar-collapse-button]').click(function() {
    if (collapsed==0)  {
    closeNav();
    }
    else  {
        openNav();
    }    

});

So that when collapsed = 1 function openNav is called and vice versa:

function openNav() {
    ...

    collapsed=0;
}

function closeNav() {
    ...

    collapsed=1;
}

However it only works the first time to call closeNav and then the button doesn't react at all.

4
  • 3
    Please post a Minimal, Complete, and Verifiable example Commented Oct 28, 2018 at 20:14
  • 1
    How did you determine it doesn't react? Did you debug it? Your code seems to work fine, I've put a console.log in the code: jsfiddle.net/5u2oxn9q Commented Oct 28, 2018 at 20:17
  • Instead of using a collapsed variable, just create a css class .closed that handles the hiding. Then $('[data-toggle=sidebar-collapse-button]').click(function() { $(this).toggleClass('closed'); }) Commented Oct 28, 2018 at 20:21
  • It is working fine for me, please check other relative issue. Commented Oct 28, 2018 at 20:25

2 Answers 2

1

As @connexo said, I would be inclined to use a toggle as well - if it is a show and hide you are trying to achieve.

If you want to use your logic, you can do something like this using a data attribute (or something similar):

Eg:

<a href="#" data-toggle="sidebar-collapse-button" data-state="0">
                    <img class="sidebar-img" id = "collapse-img" src="https://via.placeholder.com/150"/>
                    <span class="sidebar-text">Collapse</span>
 </a>

And for your script you can use:

var collapsed = 0;

$('[data-toggle=sidebar-collapse-button]').click(function() {
    var state = $(this).data('state');
    if (state == "0") {
        console.log("Closed");
      $(this).data('state','1')
    } else {
        console.log("Opened");
      $(this).data('state','0')
    }
console.log(state);
});

Further reading on data: .data()

Sign up to request clarification or add additional context in comments.

Comments

1

Here is a simple toggler in vanilla Javascript. I am not sure want you want to do or what frameworks you use but this is the basic idea

 <a href="#" onCLick="toggleSidebar()" data-toggle="sidebar-collapse-button">
  <span class="sidebar-text">Collapse</span>
 </a>

<div id="myDiv">
  Collapsed div
</div>

toggleSidebar = () => {
  let element = document.getElementById("myDiv")
  if(element.classList.contains("mystyle")){
    element.classList.remove("mystyle");
  } else  element.classList.add("mystyle");
}

1 Comment

You seem to have missed out on element.classList.toggle('mystyle').

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.