469,312 Members | 2,422 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,312 developers. It's quick & easy.

Hide multiple elements by name

Hi!

I have a dynamically generated page (PHP), which contains an Explorer
like view of items. I would like to hide multiple <tr>'s by name, but
I can't figure out how thats done.

I have this code to hide one element by id

if (document.getElementById(id)){
document.getElementById(id).style.position = 'relative';
document.getElementById(id).style.display = 'none';
}

Anyone has a cod sample for looping through multiple elements and hide
them?

Jochen
--
Jochen Daum - CANS Ltd.
PHP DB Edit Toolkit -- PHP scripts for building
database editing interfaces.
http://sourceforge.net/projects/phpdbedittk/
Jul 20 '05 #1
12 11613
Jochen Daum wrote:
Hi!

I have a dynamically generated page (PHP), which contains an Explorer
like view of items. I would like to hide multiple <tr>'s by name, but
I can't figure out how thats done.
.... Anyone has a cod sample for looping through multiple elements and hide
them?


<script type="text/javascript">
function hide(t, id) {
var tags = document.getElementsByTagName(t);
for (var i = 0; i < tags.length; i++) {
if (tags[i].id == id) {
tags[i].style.display = 'none';
}
}
}
</script>

.....
<div id="sid">A</div>
<div id="impid">B</div>
<div id="sid">C</div>
<div id="impid">D</div>

.....
<a href="#" onclick="hide('div','sid');return false">Hide divs</a>

.....

HTH;
JW

Jul 20 '05 #2
On Wed, 21 Jan 2004 23:39:17 +0100, Janwillem Borleffs <jw@jwscripts.com>
wrote:

<snip>
<div id="sid">A</div>
<div id="impid">B</div>
<div id="sid">C</div>
<div id="impid">D</div>


<snip>

Which is, of course, invalid HTML.

Mike

--
Michael Winter
M.******@blueyonder.co.invalid (replace ".invalid" with ".uk" to reply)
Jul 20 '05 #3
Michael Winter wrote:
Which is, of course, invalid HTML.


What is invalid about that?

JW

Jul 20 '05 #4
Hi Janwillem!

On Wed, 21 Jan 2004 23:39:17 +0100, "Janwillem Borleffs"
<jw@jwscripts.com> wrote:
Jochen Daum wrote:
Hi!

I have a dynamically generated page (PHP), which contains an Explorer
like view of items. I would like to hide multiple <tr>'s by name, but
I can't figure out how thats done.
...
Anyone has a cod sample for looping through multiple elements and hide
them?


<script type="text/javascript">
function hide(t, id) {
var tags = document.getElementsByTagName(t);
for (var i = 0; i < tags.length; i++) {
if (tags[i].id == id) {
tags[i].style.display = 'none';
}
}
}
</script>

Wouldn't that also be working with any tag, if I did it like this. It
doesn't give me an error, but it doesn't work either:

function hide_elements_by_name(me,id, pluspic){

var tags = document.getElementsByName(id);
for (var i = 0; i < tags.length; i++) {
if (tags[i].name == id) {
tags[i].style.display = 'none';
}
}
}

and the tags being:

<tr name="xyz">

??

Jochen

....
<div id="sid">A</div>
<div id="impid">B</div>
<div id="sid">C</div>
<div id="impid">D</div>

....
<a href="#" onclick="hide('div','sid');return false">Hide divs</a>

....

HTH;
JW


--
Jochen Daum - CANS Ltd.
PHP DB Edit Toolkit -- PHP scripts for building
database editing interfaces.
http://sourceforge.net/projects/phpdbedittk/
Jul 20 '05 #5
Michael Winter wrote:
Which is, of course, invalid HTML.


Never mind, see what you mean.

JW

Jul 20 '05 #6
Hi!

On Thu, 22 Jan 2004 00:27:04 +0100, "Janwillem Borleffs"
<jw@jwscripts.com> wrote:
Michael Winter wrote:
Which is, of course, invalid HTML.


Never mind, see what you mean.

Does he mean the double use of an id?

Jochen
--
Jochen Daum - CANS Ltd.
PHP DB Edit Toolkit -- PHP scripts for building
database editing interfaces.
http://sourceforge.net/projects/phpdbedittk/
Jul 20 '05 #7
Jochen Daum wrote:
Does he mean the double use of an id?


Yep
Jul 20 '05 #8
Jochen Daum wrote:
Wouldn't that also be working with any tag, if I did it like this. It
doesn't give me an error, but it doesn't work either:

function hide_elements_by_name(me,id, pluspic){

var tags = document.getElementsByName(id);


AFAIK, document.getElementsByTagName only applies to form elements.
JW

Jul 20 '05 #9
"Janwillem Borleffs" <jw@jwscripts.com> writes:

var tags = document.getElementsByName(id);


AFAIK, document.getElementsByTagName only applies to form elements.


No, any DOM Node has getElementsByTagName as a method. If you meant
getElementsByName, DOM 2 says:

getElementsByName
With [HTML 4.01] documents, this method returns the (possibly empty)
collection of elements whose name value is given by elementName. In
[XHTML 1.0] documents, this methods only return the (possibly empty)
collection of form controls with matching name. This method is case
sensitive.

So yes and no, in XHTML it's only form controls. In HTML, it's
anything with a name attribute.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #10
Janwillem Borleffs wrote:
Jochen Daum wrote:
Wouldn't that also be working with any tag, if I did it like this. It
doesn't give me an error, but it doesn't work either:

function hide_elements_by_name(me,id, pluspic){

var tags = document.getElementsByName(id);

AFAIK, document.getElementsByTagName only applies to form elements.


<div>test</div>
<script type="text/javascript">
alert(document.getElementsByTagName('div'))
</script>

IE6: alerts [object]
Opera 7: alerts [object nodeList]
NS7: alerts [object HTMLCollection]

<script type="text/javascript">
alert(document.getElementsByName('myDiv'))
</script>

Gives similar results.

I hunted a reference on MSDN but obviously didn't know where to hunt it :(

--
Randy
Chance Favors The Prepared Mind

Jul 20 '05 #11
On Thu, 22 Jan 2004 00:46:28 +0100, Janwillem Borleffs <jw@jwscripts.com>
wrote:
Jochen Daum wrote:
Wouldn't that also be working with any tag, if I did it like this. It
doesn't give me an error, but it doesn't work either:

function hide_elements_by_name(me,id, pluspic){

var tags = document.getElementsByName(id);


AFAIK, document.getElementsByTagName only applies to form elements.


Using

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">

<html lang="en" dir="ltr">
<head>
<meta http-equiv="Content-Type"
content="text/html; charset=ISO-8859-1">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<meta http-equiv="Content-Style-Type" content="text/css">
...

and calling

alert( document.getElementsByTagName('meta').length );

returns '3'.

Mike

--
Michael Winter
M.******@blueyonder.co.invalid (replace ".invalid" with ".uk" to reply)
Jul 20 '05 #12
Hi!

On Wed, 21 Jan 2004 19:17:37 -0500, Randy Webb
<hi************@aol.com> wrote:
Janwillem Borleffs wrote:
Jochen Daum wrote:
Wouldn't that also be working with any tag, if I did it like this. It
doesn't give me an error, but it doesn't work either:

function hide_elements_by_name(me,id, pluspic){

var tags = document.getElementsByName(id);

AFAIK, document.getElementsByTagName only applies to form elements.


<div>test</div>
<script type="text/javascript">
alert(document.getElementsByTagName('div'))
</script>

IE6: alerts [object]
Opera 7: alerts [object nodeList]
NS7: alerts [object HTMLCollection]

<script type="text/javascript">
alert(document.getElementsByName('myDiv'))
</script>

Gives similar results.

I hunted a reference on MSDN but obviously didn't know where to hunt it :(


http://msdn.microsoft.com/workshop/a...entsbyname.asp

Like always, its not fully described. It returns a collection, but an
empty one.

This works
var tags = document.body.getElementsByTagName('tr');

for (var i = 0; i < tags.length; i++) {
if (tags[i].name == id) {
tags[i].style.display = 'none';
}
}

and doesn't work with getElementsByName


HTH, Jochen
--
Jochen Daum - CANS Ltd.
PHP DB Edit Toolkit -- PHP scripts for building
database editing interfaces.
http://sourceforge.net/projects/phpdbedittk/
Jul 20 '05 #13

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by MOHSEN KASHANI | last post: by
2 posts views Thread by Eero Tuomenoksa | last post: by
5 posts views Thread by ali | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.