How can I change CSS display none or block property using jQuery?
15 Answers
The correct way to do this is to use show and hide:
$('#id').hide();
$('#id').show();
An alternate way is to use the jQuery css method:
$("#id").css("display", "none");
$("#id").css("display", "block");
8 Comments
#id used for ids what about if I want to use class?$(".class").css("display", "none");$('#id').css('display', 'none');, but it did not work. However, using $('#id').css('color', 'red'); does work. I am not sure why, though. Does anyone have any ideas? Thanks in advance.There are several ways to accomplish this, each with its own intended purpose.
1.) To use inline while simply assigning an element a list of things to do
$('#ele_id').css('display', 'block').animate(....
$('#ele_id').css('display', 'none').animate(....
2.) To use while setting multiple CSS properties
$('#ele_id').css({
display: 'none'
height: 100px,
width: 100px
});
$('#ele_id').css({
display: 'block'
height: 100px,
width: 100px
});
3.) To dynamically call on command
$('#ele_id').show();
$('#ele_id').hide();
4.) To dynamically toggle between block and none, if it's a div
$('#ele_id').toggle();
Comments
If the display of the div is block by default, you can just use .show() and .hide(), or even simpler, .toggle() to toggle between visibility.
1 Comment
Simple way:
function displayChange(){
$(content_id).click(function(){
$(elem_id).toggle();}
)}
1 Comment
(function($){
$.fn.displayChange = function(fn){
$this = $(this);
var state = {};
state.old = $this.css('display');
var intervalID = setInterval(function(){
if( $this.css('display') != state.old ){
state.change = $this.css('display');
fn(state);
state.old = $this.css('display');
}
}, 100);
}
$(function(){
var tag = $('#content');
tag.displayChange(function(obj){
console.log(obj);
});
})
})(jQuery);
1 Comment
In my case I was doing show / hide elements of a form according to whether an input element was empty or not, so that when hiding the elements the element following the hidden ones was repositioned occupying its space it was necessary to do a float: left of the element of such an element. Even using a plugin as dependsOn it was necessary to use float.
Comments
Use this:
$("#id").(":display").val("block");
Or:
$("#id").(":display").val("none");
1 Comment
<style> #choosepath { display: none; } </style> <script> $(document).ready(function () { $('#btn_journey').click(function () { alert("button click"); $('#choosepath').css({"display":"normal"}); }); }); </script> </head> <body> <a href="#" class="btn btn-primary btn-lg" id="btn_journey">Start your journey</a> <div class="col-lg-3 col-md-6 mb-4" id="choosepath"> </div> </body></html>you can do it by button
<button onclick"document.getElementById('elementid').style.display = 'none or block';">