Welcome to WebmasterWorld Guest from 3.81.28.94

Forum Moderators: open

Message Too Old, No Replies

.style.display = 'none'; not working

javascript style display = 'none' help

     
4:01 pm on Jul 30, 2007 (gmt 0)

New User

10+ Year Member

joined:July 30, 2007
posts:5
votes: 0


<input type="button" onClick="document.mainform.main.style.display = 'none';">
<form name="mainform">
<div name="main">
content to be dissapeared on button click
</div>
thats basically the code im trying to make but whenever im doing it it doesnt work... im using windows vista and am on the latest version of IE.. any help would be greatly appreciated
4:13 pm on July 30, 2007 (gmt 0)

Preferred Member

10+ Year Member

joined:July 5, 2005
posts:352
votes: 0


It's probably easier to just give your div an ID and access it that way.

<input type="button" onclick="document.getElementById('main').style.display = 'none';">

<div id="main">
content to be dissapeared on button click
</div>

4:33 pm on July 30, 2007 (gmt 0)

Full Member

10+ Year Member

joined:July 13, 2007
posts:235
votes: 0


Yes, I believe this is your problem. I don't think the name attribute has any effect on non-input elements.
4:45 pm on July 30, 2007 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member fotiman is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Oct 17, 2005
posts:5021
votes: 26


Correct. The name attribute is invalid on your div element. Try this:

<input type="button" onclick="function(){document.getElementById("main").style.display = 'none';}";
<form name="mainform">
<div id="main">
content to be dissapeared on button click
</div>

6:16 pm on July 30, 2007 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Apr 15, 2004
posts:2047
votes: 0


Fotiman made an uncharacteristic little slip or two.
Change the top line to:

<input type="button" onclick="document.getElementById('main').style.display = 'none';">

7:22 pm on July 30, 2007 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member fotiman is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Oct 17, 2005
posts:5021
votes: 26


Aye, that I did. :) Not used to working with inline attributes... I've come to avoid them whenever possible.
5:53 pm on Aug 16, 2007 (gmt 0)

New User

10+ Year Member

joined:July 30, 2007
posts:5
votes: 0


thanks everyone i didnt know it had to be getElementById for style but i figured it out thanx
6:51 pm on Aug 16, 2007 (gmt 0)

Junior Member

10+ Year Member

joined:Nov 18, 2004
posts:61
votes: 0


thanks everyone i didnt know it had to be getElementById for style but i figured it out thanx

you don't have to use getElementById for style but it's easier to find your object by id since id are unique.

you could always use getElementsByName which will create an array of all names you ask it to look for in the document and set style to that.

this (DOM:document.getElementsByName [developer.mozilla.org]) shows you how to use it though using getElementById is probably your best bet.