Connecting Tech Pros Worldwide Help | Site Map

Manipulate all tags

Fabian
Guest
 
Posts: n/a
#1: Jul 20 '05
Ho can I create a function in javascript to dynamically toggle all <em>
tags between bold and italic?


--
--
Fabian
Visit my website often and for long periods!
http://www.lajzar.co.uk

Fabian
Guest
 
Posts: n/a
#2: Jul 20 '05

re: Manipulate all tags


Fabian hu kiteb:
[color=blue]
> Ho can I create a function in javascript to dynamically toggle all
> <em> tags between bold and italic?[/color]

Specifically, without adding name or id attributes to these tags, is it
possible to find all <em> (or whatever) tags?


--
--
Fabian
Visit my website often and for long periods!
http://www.lajzar.co.uk

Evertjan.
Guest
 
Posts: n/a
#3: Jul 20 '05

re: Manipulate all tags


Fabian wrote on 07 nov 2003 in comp.lang.javascript:
[color=blue]
> Fabian hu kiteb:
>[color=green]
>> Ho can I create a function in javascript to dynamically toggle all
>> <em> tags between bold and italic?[/color]
>
> Specifically, without adding name or id attributes to these tags, is it
> possible to find all <em> (or whatever) tags?[/color]

In IE:

var coll = document.all.tags("DIV");
if (coll!=null) {
for (i=0; i<coll.length; i++) {
coll[i].style.fontSize = "7pt"
coll[i].style.color = "black"
}
}



--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Lasse Reichstein Nielsen
Guest
 
Posts: n/a
#4: Jul 20 '05

re: Manipulate all tags


"Fabian" <lajzar@hotmail.com> writes:
[color=blue]
> Fabian hu kiteb:
>[color=green]
> > Ho can I create a function in javascript to dynamically toggle all
> > <em> tags between bold and italic?[/color]
>
> Specifically, without adding name or id attributes to these tags, is it
> possible to find all <em> (or whatever) tags?[/color]

Yes. In standard compliant browsers, and IE from version 5, you can use

Setting all ems italic:
var ems = document.getElementsByTagName("em");
for (var i=0;i<ems.length;i++) {
ems[i].style.fontStyle = "italic";
ems[i].style.fontWeight = "bold";
}

It is probably simpler to just create some CSS classes:
<style type="text/css">
em.bold {
font-weight:bold;
font-style:normal;
}
em.italic {
font-weight:normal;
font-style:italic;
}
</style>

You can then change all ems to one or the other:

var ems = document.getElementsByTagName("em");
for (var i=0;i<ems.length;i++) {
ems[i].className = "italic"; // or "bold"
}

If you care about IE 4, you can use
var ems = document.all.tags("em")
when the preferred document.getElementsByTagName isn't available.

var ems;
if (document.getElementsByTagName) {
ems = document.getElementsByTagName("em");
} else if (document.all && document.all.tags) {
ems = document.all.tags("em");
} else { // panic!
}
for (var i=0;i<ems.length;i++) {
ems[i].className = "italic"; // or "bold"
}

/L
--
Lasse Reichstein Nielsen - lrn@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Closed Thread