Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

hide div by javascript

7 views
Skip to first unread message

cerr

unread,
Jan 19, 2012, 4:27:11 PM1/19/12
to
Hi There,

I'm trying to hide a div when my java script gets executed but it
doesn't seem to work.
My script looks like this, I see the message test popup but the div
doesn't disappear.
function closediv()
{

var x=document.getElementsByName("emaildiv");
if(x)
{
alert("test");
x.style.visibility = 'hidden';
}
}
However when i add visibility:hidden; to my style declaration it seems
to work well. What's going on here?
Any hints or suggestions are appreciated!
URL:http://pizzalita.com/index1.html

Jonathan N. Little

unread,
Jan 19, 2012, 5:46:42 PM1/19/12
to
DIVs don't have names. Use an ID.


<div id="emaildiv" ...

and then change your JavaScript:

var x=document.getElementsByID("emaildiv");


Also note that getElementsByName() returns a collection not a single
element like getElementsByTagName() so to test for success would be


if( x.length ) { ...

and to change the property of first element would be:

x[1].style.visibility = 'hidden';


--
Take care,

Jonathan
-------------------
LITTLE WORKS STUDIO
http://www.LittleWorksStudio.com

Gregor Kofler

unread,
Jan 19, 2012, 6:30:10 PM1/19/12
to
Am 2012-01-19 23:46, Jonathan N. Little meinte:
> cerr wrote:
>> Hi There,
>>
>> I'm trying to hide a div when my java script gets executed but it
>> doesn't seem to work.
>> My script looks like this, I see the message test popup but the div
>> doesn't disappear.
>> function closediv()
>> {
>>
>> var x=document.getElementsByName("emaildiv");
>> if(x)
>> {
>> alert("test");
>> x.style.visibility = 'hidden';
>> }
>> }
>> However when i add visibility:hidden; to my style declaration it seems
>> to work well. What's going on here?
>> Any hints or suggestions are appreciated!
>> URL:http://pizzalita.com/index1.html
>
> DIVs don't have names. Use an ID.
>
>
> <div id="emaildiv" ...
>
> and then change your JavaScript:
>
> var x=document.getElementsByID("emaildiv");

The name of the method is getElementById().

>
> Also note that getElementsByName() returns a collection not a single
> element like getElementsByTagName() so to test for success would be
>
>
> if( x.length ) { ...
>
> and to change the property of first element would be:
>
> x[1].style.visibility = 'hidden';

This ain't some obscure language (like ColdFusion), the first element
would be x[0].

Gregor


--
http://vxweb.net


Jonathan N. Little

unread,
Jan 19, 2012, 9:17:44 PM1/19/12
to
Gregor Kofler wrote:
> Am 2012-01-19 23:46, Jonathan N. Little meinte:

>> var x=document.getElementsByID("emaildiv");
>
> The name of the method is getElementById().
>

Yup, finger too long on the shift key!


>> and to change the property of first element would be:
>>
>> x[1].style.visibility = 'hidden';
>
> This ain't some obscure language (like ColdFusion), the first element
> would be x[0].

:-D Doh! Right, my head has been in old VB coding lately...
0 new messages