Manipulate all tags

F

Fabian

Ho can I create a function in javascript to dynamically toggle all <em>
tags between bold and italic?
 
F

Fabian

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

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

Evertjan.

Fabian wrote on 07 nov 2003 in comp.lang.javascript:
Fabian hu kiteb:


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

In IE:

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

Lasse Reichstein Nielsen

Fabian said:
Fabian hu kiteb:


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

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.style.fontStyle = "italic";
ems.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.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.className = "italic"; // or "bold"
}

/L
 

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
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top