Advertisement
Advertisement


How can I change CSS display none or block property using jQuery?


Question

How can I change CSS display none or block property using jQuery?

2020/05/06
1
445
5/6/2020 1:55:33 PM

Accepted Answer

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");
2013/10/16
955
10/16/2013 12:19:58 PM

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

  • some elements are displayed as inline, inline-block, or table, depending on the Tag Name

$('#ele_id').toggle();

2020/05/06

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.

2010/08/27

For hide:

$("#id").css("display", "none");

For show:

$("#id").css("display", "");

Other way to do it using jQuery CSS method:

$("#id").css({display: "none"});
$("#id").css({display: "block"});
2012/02/29

Simple way:

function displayChange(){
$(content_id).click(function(){
  $(elem_id).toggle();}

)}
2014/06/10

Source: https://stackoverflow.com/questions/3582619
Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Email: [email protected]