Hide multiple elements by name

J

Jochen Daum

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
 
J

Janwillem Borleffs

Jochen said:
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.id == id) {
tags.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
 
J

Jochen Daum

Hi Janwillem!

Jochen said:
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.id == id) {
tags.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.name == id) {
tags.style.display = 'none';
}
}
}

and the tags being:

<tr name="xyz">

??

Jochen
 
J

Janwillem Borleffs

Jochen said:
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
 
R

Randy Webb

Janwillem said:
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 :(
 
L

Lasse Reichstein Nielsen

Janwillem Borleffs said:
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
 
M

Michael Winter

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
 
J

Jochen Daum

Hi!

Janwillem said:
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/author/dhtml/reference/methods/getelementsbyname.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.name == id) {
tags.style.display = 'none';
}
}

and doesn't work with getElementsByName




HTH, Jochen
 

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

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top