Newbie: Changing value of css value.

J

JJ Harrison

How can i use javascript to change:

<div style="display: none">

</div>

to:

<div style="display: block">

</div>
 
C

Csaba2000

Somehow you have to get a reference to the div.
A standard way is to give it an id like so:

<div id=myFavoriteIdName
style="display:none">My div text</div>

and then in your javascript you can do:
var myDiv = document.getElementById("myFavoriteIdName");
myDiv.style.display = "block";

Csaba Gabor from New York
 
J

JJ Harrison

(Page inline below). I have written a function that swaps the display when i
click on the paragraph with BLAH written in it. This works fine on the first
click. However on the second click it doesn't work. What am i doing wrong?

Heres my page:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
<title>Untitled</title>
<script language="JavaScript" type="text/javascript">
<!--
function swapdisplay(element)
{
if(element.style.display = "none")
{
element.style.display = "block";
} else {
element.style.display = "none";
}
}

//-->
</script>

</head>
<body>
<p onmousedown="swapdisplay(document.getElementById('docdrop'));">
BLAH
</p>
<div id="docdrop" style="display:none">
Blah Blah
</div>
</body>
</html>
 
C

Csaba2000

You need a double equal (==) when testing for equality:
if (element.style.display=="none")

Also, as an aside, if you've only got standard characters
in an attribute, you don't need the quotes. Thus:
<div id=docdrop style="display:none">

Csaba
 
L

Lasse Reichstein Nielsen

Csaba2000 said:
Also, as an aside, if you've only got standard characters
in an attribute, you don't need the quotes. Thus:
<div id=docdrop style="display:none">

I recommend putting them there anyway. If you always quote your
values, you are less likely to forget the quotes when they are
required. Can you, without looking it up, tell me which of the
following values need quoting:
abe-mad
abe_mad
abe:mad
abe;mad
abe,mad
abe.mad
abe/mad
abe1mad
abe&amp;mad
abe(mad)
abe[mad]

The answer is in
<URL:http://www.w3.org/TR/html4/intro/sgmltut.html#h-3.2.2>, which
also goes on to say: "We recommend using quotation marks even when it
is possible to eliminate them."

It is also a good habit if you ever change to XHTML, where all
attribute values must be quoted.

(and, quoting DU in the message just above this one in my reader:
---
http://www.htmlhelp.org/faq/html/basics.html#quotes
"By default, SGML requires that all attribute values be delimited
using either double quotation marks (...)"

http://www.w3.org/TR/html4/intro/sgmltut.html#attributes
Why attribute values should always be quoted in HTML
http://www.cs.tut.fi/~jkorpela/qattr.html
---
Thanks DU.)

(And as an aside, please trim your quotes, and preferably answer below
the relevant quote. It makes reading much easier).
/L
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top