Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setAttribute('display','none') not working

Tags:

javascript

dom

function classInfo(e){
    document.getElementById('classRight').setAttribute('display','none');
    alert(e);   
}

I think this code is very straightforward bit it's not working, and is not hiding the element in question. I am getting my alert, which makes me think there is no issue.

Any help would be appreciated.

like image 246
jason m Avatar asked Feb 12 '11 23:02

jason m


People also ask

How do I show display none in CSS?

The default display value for most elements is block or inline . This panel contains a <div> element, which is hidden by default ( display: none ). It is styled with CSS, and we use JavaScript to show it (change it to ( display: block ).

How do you use setAttribute?

setAttribute() Sets the value of an attribute on the specified element. If the attribute already exists, the value is updated; otherwise a new attribute is added with the specified name and value. To get the current value of an attribute, use getAttribute() ; to remove an attribute, call removeAttribute() .

How do I change the display none style?

Answer: Use the jQuery css() Method You can use the jQuery css() method to change the CSS display property value to none or block or any other value. The css() method apply style rules directly to the elements i.e. inline.

What is the user in the setAttribute () method?

The setAttribute() method is used to set or add an attribute to a particular element and provides a value to it. If the attribute already exists, it only set or changes the value of the attribute. So, we can also use the setAttribute() method to update the existing attribute's value.


1 Answers

display is not an attribute - it's a CSS property. You need to access the style object for this:

document.getElementById('classRight').style.display = 'none';
like image 102
Yi Jiang Avatar answered Sep 19 '22 08:09

Yi Jiang