1

I have a button that when pressed hides a div using 'Display: None;'. How do I write in jquery to make the css property toggle between Display: None and Display: Block?

$('#help_btn').click(function(){ $('#help_box').css('display', 'none'); }); 

when you click it again

$('#help_box').css('display', 'block'); 

so on and so forth. Thanks!

edit:

My css would look like this

#help_box { display: block; } 

and my html is

<div id='help_box><p> Some helpful info!</p></div> <button id='help_btn>help?</button> 
2
  • 2
    I recommend defining those display settings using a CSS class and then using jQuery's toggleClass(). Can we see your relevant HTML and CSS, as well? Commented Sep 26, 2018 at 20:07
  • Why #help_box is inline? Commented Sep 26, 2018 at 20:11

5 Answers 5

1

You could toggle CSS classes as commented by @showdev. Or you could try this

$('#help_btn').click(function() { if($('#help_box').is(':visible')) { $('#help_box').css('display', 'none'); } else { $('#help_box').css('display', 'inline'); } }); 
Sign up to request clarification or add additional context in comments.

Comments

1

Use toggleClass(); as other answers suggest or use toggle();

$('#help_btn').click(function(){ $('#help_box').toggle(); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="help_box"><p> Some helpful info!</p></div> <button id="help_btn">help?</button>
Note that you have typo in your html code, you've got to properly double-quote id of your elements.

Comments

0

You can use toggleClass() to toggle two classes.

ToggleClass logic is that we add the missing class and we remove the previous one.

$('#help_btn').click(function(){ $('#help_box').toggleClass("hidden shown"); });
.shown { display: inline; } .hidden { display: none; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button id="help_btn">Change</button> <div id="help_box"><p> Some helpful info!</p></div>

Comments

0

Create any class

.someclass{ display: inline}

Then toggle class

$('#help_btn').click(function(){ $('#help_box').toggleClass("someClass"); }); 

Comments

0

There are two common ways:

The first one is by checking the css property and reversing it, something like this:

$(".something-i-clicked").on("click", function() { if($(".my-class").css("display") == "none") { // if display: none; is set to .my-class // reverse it to display: block; $(".my-class").css("display", "block"); } else { // it has display: block; // reverse it to display: none; $(".my-class").css("display", "none"); } }) 

The second way is by toggling a modifier css class, maybe like this:

// my_css_file.css .my-class { display: none; } .d-block { display: block; } // my_js_file.js $(".something-i-clicked").on("click", function() { // toggle the class $(".my-class").toggleClass("d-block"); }) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.