How do I change the CSS property “display” in javascript

How do I change the CSS property display, in JavaScript, from display:none to display:normal for these divs?

#hide_0 { display:none }
#hide_1 { display:none }
#hide_2 { display:none }
#hide_3 { display:none }
#hide_4 { display:none }
#hide_5 { display:none }

Only one at a time. I need to display one and hide the rest.

What I used:

var persistent_element='hide_1';

function link_update(link_display)
  var local_element;

How I connected it : m4 is a minified - connects onclick to these methods

m4('l1',function {return link_update(hide_1);}); 
m4('l2',function {return link_update(hide_2);});
m4('l3',function {return link_update(hide_3);});
m4('l4',function {return link_update(hide_4);});
m4('l5',function {return link_update(hide_5);});
m4('l6',function {return link_update(hide_6);});



To use javascript to change the style, you can do it like this:

// hide an element
document.getElementById("hide_0").style.display = "none";

// show a block element
document.getElementById("hide_1").style.display = "block";

// to go back to the default or CSS specified value
document.getElementById("hide_2").style.display = "";

So, if you wanted to hide all and show one, you could do that with this function:

function showOneHideOthers(base, len, numToShow) {
    // objects must have ids like base_0, base_1, etc...
    for (var i = 0; i < len; i++) {
        if (i != numToShow) {
            document.getElementById(base+i).style.display = "none";
    document.getElementById(base+numToShow).style.display = "block";

showOneHideOther("hide_", 6, 2);

P.S. normal is not a valid value for the display property. The typical values are block, none and inline and there are others like inline-block, table, etc....


Your question is not particularly clear, but the essence of what you want to do is simple. You can get a reference to a DOM element which has an id using getElementById, and you can change the display property:

document.getElementById("hide_0").style.display = "none"; //or "block"

However, you have several element that you want to hide/show (I'm not sure when you want to do so), so it may be easier to use a different method of selecting the elements (such as getElementsByTagName, or getElementsByClassName, but it depends on your HTML and what you're actually trying to do).


You can set a css property on an element using the style method. = '';


Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.