PDA

View Full Version : How to replace table if...


netbeui
01-05-2001, 01:58 PM
Hi,
How can I replace or comment out an HTML table if the name of the table is
like adidas (upper and lowercase)?

<code>
<Table name=" blah blah blah adidas blah blah ">
</table>
</code>

please post here and email me as well.

Thanks,
Lou.

whkoh
01-05-2001, 10:13 PM
Please clarify. I don't get what you're trying to say.

------------------
Koh Wei Han
Network Engineer
Contact: whkoh@apexmail.com , whkoh@mailandnews.com , whkoh@020.co.uk

kdjoergensen
01-08-2001, 11:44 AM
LATEST UPDATE: I HAVE MADE SOME SLIGHT CORRECTIONS TO THE BELOW. IT SHOULD WORK NOW.

In DOM (MSIE5/netscape6) to get a list of all tables in the page (an array):


var myTables = (document.getElementById) ? document.getElementsByTagName('TABLE') : document.all.tags("TABLE");

for (var i=0;i<myTables.length;i++){
var currID = (myTables[i].id).toLowerCase();
if (currID){ //test if table has an ID
if (currID.indexOf('addidas') != -1) {
//table with id containing addidas found
myTables[i].STYLE.display = 'none';
}
}
}


All you do is to cycle through each of the tables and test for the presence of the search string 'addidas' within the main string (id) which I here convert toLowerCase to make comparsion valid even if upper and lower case does not match excactly.


Line by line:
for (var i=0;i<myTables.length;i++){
--> cycle through all tables in the array. The tables were extracted and stuffed into the arrray by the getElementByTagName method.


var currID = (myTables[i].id).toLowerCase;
--> extract the (string value of the) id of the table (if available) and convert (the string) to lower case for easy comparison.


if (currId.indexOf('addidas') != -1) {
--> using indexOf to test for a search string (here: addidas) will return a value representing the character at which point a positive match starts (zero based). e.g. if you searched for "mydoghouse".index("dog") a return value of 2 would be returned (dog in mydoghouse starts at the 3rd character which means value is 2 (zero based: 0,1,2 = 3rd character).
If the search string could NOT be found a return value of -1 would be returned. The construction (!= -1) simply states: if return value different from (!=) -1 then match is found (we dont care about where in the id the match is found as long as it is found, right ?)


myTables[i].style.display = 'none';
--> finally we set the objects display property to none (we hide the table).

Note above does not work in netscape4. IE4 uses slightly different syntax, but both MSIE5 and Netscape6 will work with above.


** UPDATE **
To make it work in MSIE4, all you need to REPLACE folowing line:
var myTables = document.getElementsByTagName('TABLE');

var myTables = document.all.tags("TABLE");


[This message has been edited by kdjoergensen (edited 01-11-2001).]