Do X if element is Y 
November 17th, 2008, 01:05 PM
| | | |
How do I find if an element is a certain element (like
HTMLTableRowElement) ?
Using "alert(typeof el)" simply displays "object" while "alert(el)"
displays "HTMLTableRowElement".
el = document.getElementById('stringValue');
I need to match this specific element because of problems with
"display: none/block" in non-IE browsers.
This doesnt work:
if (el == HTMLTableRowElement || el == 'HTMLTableRowElement') {
el.style.display = '';
} else {
el.style.display = 'block';
} | 
November 17th, 2008, 01:15 PM
| | | | re: Do X if element is Y
Kim wrote: Quote:
How do I find if an element is a certain element (like
HTMLTableRowElement) ?
| Assuming you know you script an (X)HTML element then
if (el.tagName.toLowerCase() === 'tr')
should suffice.
--
Martin Honnen http://JavaScript.FAQTs.com/ | 
November 17th, 2008, 01:35 PM
| | | | re: Do X if element is Y
On Nov 17, 1:00 pm, Kim wrote: Quote:
How do I find if an element is a certain element (like
HTMLTableRowElement) ?
| The element's - tagName - or - nodeName - properties would seem like a
good place to start (remembering that despite any possible impressions
to the contrary given by the mark-up, the document likely is an HTML
document and so those properties values will be case-normalised to
uppercase). Quote:
Using "alert(typeof el)" simply displays "object" while
"alert(el)" displays "HTMLTableRowElement".
el = document.getElementById('stringValue');
>
I need to match this specific element because of problems with
"display: none/block" in non-IE browsers.
>
This doesnt work:
if (el == HTMLTableRowElement || el == 'HTMLTableRowElement') {
el.style.display = '';
| <snip>
If your CSS and mark-up are such that the above works anywhere (which
would be expected) then it will also work fine on IE. That is, you
don't need to test and branch, you just assign the empty string to the
display property and the display state will revert to its normal/
default value (all else being equal). | 
November 17th, 2008, 01:35 PM
| | | | re: Do X if element is Y
On Nov 17, 2:07*pm, Martin Honnen <mahotr...@yahoo.dewrote: Quote:
Kim wrote: Quote:
How do I find if an element is a certain element (like
HTMLTableRowElement) ?
| >
Assuming you know you script an (X)HTML element then
* *if (el.tagName.toLowerCase() === 'tr')
should suffice.
>
--
>
* * * * Martin Honnen
* * * *http://JavaScript.FAQTs.com/ | Thanks, that works fine. Its XHTML 1.0 T.
If I have to add additional elements later, then is there an easier
solution then using multiple if-statements ? | 
November 17th, 2008, 01:45 PM
| | | | re: Do X if element is Y
Kim wrote: Quote:
If I have to add additional elements later, then is there an easier
solution then using multiple if-statements ?
| switch (el.tagName.toLowerCase())
{
case 'tr':
...
break;
case 'td':
...
break;
}
--
Martin Honnen http://JavaScript.FAQTs.com/ | 
November 17th, 2008, 02:55 PM
| | | | re: Do X if element is Y
On Nov 17, 11:32*am, Kim <kims...@gmail.comwrote: Quote:
On Nov 17, 2:07*pm, Martin Honnen <mahotr...@yahoo.dewrote:
If I have to add additional elements later, then is there an easier
solution then using multiple if-statements ?
| I have a way simpler solution. Put a class in your table via
javascript :
var t = document.getElementById('myTable');
t.className = t.className ? t.className + 'foo' : 'foo';
Then hide the elements via CSS:
..foo tr {
display: none;
}
Cheers
--
Gabriel Gilini | 
November 17th, 2008, 03:25 PM
| | | | re: Do X if element is Y
On Nov 17, 2:27*pm, Henry <rcornf...@raindrop.co.ukwrote: Quote:
On Nov 17, 1:00 pm, Kim wrote:
> Quote:
How do I find if an element is a certain element (like
HTMLTableRowElement) ?
| >
The element's - tagName - or - nodeName - properties would seem like a
good place to start (remembering that despite any possible impressions
to the contrary given by the mark-up, the document likely is an HTML
document and so those properties values will be case-normalised to
uppercase).
> Quote:
Using "alert(typeof el)" simply displays "object" while
"alert(el)" displays "HTMLTableRowElement".
el = document.getElementById('stringValue');
| > Quote:
I need to match this specific element because of problems with
"display: none/block" in non-IE browsers.
| > Quote:
This doesnt work:
if (el == HTMLTableRowElement || el == 'HTMLTableRowElement') {
* * * * el.style.display = '';
| >
<snip>
>
If your CSS and mark-up are such that the above works anywhere (which
would be expected) then it will also work fine on IE. That is, you
don't need to test and branch, you just assign the empty string to the
display property and the display state will revert to its normal/
default value (all else being equal).
| Thanks for the explanation why "display: ''" works. Guess I can remove
the "else" now.
@Martin: I was thinking more of using an array. A switch simply makes
it look nicer.
@Gabriel: You got it wrong. I dont want to hide the whole table - just
a part of it. And without making classes for each table I do this in.
Problem is solved. | 
November 17th, 2008, 11:55 PM
| | | | re: Do X if element is Y
On 17 nov, 13:20, Kim <kims...@gmail.comwrote: Quote:
On Nov 17, 2:27*pm, Henry <rcornf...@raindrop.co.ukwrote:
@Gabriel: You got it wrong. I dont want to hide the whole table - just
a part of it. And without making classes for each table I do this in.
| Well, is the part you want to hide always the same?
Because if it is, you can still do what I said.
You just set a class to the elements you want hidden, like 'hide' | 
November 18th, 2008, 12:05 AM
| | | | re: Do X if element is Y
On 17 nov, 21:53, Gabriel Gilini <gabr...@usosim.com.brwrote: Quote:
On 17 nov, 13:20, Kim <kims...@gmail.comwrote:
> Quote:
On Nov 17, 2:27*pm, Henry <rcornf...@raindrop.co.ukwrote:
@Gabriel: You got it wrong. I dont want to hide the whole table - just
a part of it. And without making classes for each table I do this in.
| >
Well, is the part you want to hide always the same?
Because if it is, you can still do what I said.
>
You just set a class to the elements you want hidden, like 'hide'
| Sorry, sent it by accident.
Like I was saying, let's say you have this table
<table id="myTable">
<tr class="hide">
<td>foo</td>
</tr>
<tr>
<td>bar</td>
</tr>
<tr>
<td>baz</td>
</tr>
</table> Quote:
var t = document.getElementById('myTable');
t.className = t.className ? t.className + 'foo' : 'foo';
| Still the same
..foo tr.hide { And there you go
Cheers
--
Gabriel Gilini | 
November 18th, 2008, 01:35 AM
| | | | re: Do X if element is Y
On Nov 17, 11:00*pm, Kim <kims...@gmail.comwrote: Quote:
How do I find if an element is a certain element (like
HTMLTableRowElement) ?
>
Using "alert(typeof el)" simply displays "object" while "alert(el)"
displays "HTMLTableRowElement".
el = document.getElementById('stringValue');
>
I need to match this specific element because of problems with
"display: none/block" in non-IE browsers.
>
This doesnt work:
if (el == HTMLTableRowElement || el == 'HTMLTableRowElement') {
* * * * el.style.display = '';} else {
>
* * * * el.style.display = 'block';
>
}
| Toggle between '' (empty string) and 'none', then you don't care about
the default style. Setting style.display to '' will return the
element's display property to its default value, whatever that might
be. Setting it to 'none' will hide it and remove it from the document
flow so it takes up zero space.
--
Rob |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 225,662 network members.
|