By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,963 Members | 1,749 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,963 IT Pros & Developers. It's quick & easy.

Manipulate all tags

P: n/a
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

Jul 20 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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?
--
--
Fabian
Visit my website often and for long periods!
http://www.lajzar.co.uk

Jul 20 '05 #2

P: n/a
Fabian wrote on 07 nov 2003 in comp.lang.javascript:
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?


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)
Jul 20 '05 #3

P: n/a
"Fabian" <la****@hotmail.com> writes:
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?


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 - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.