Manipulate all tags

Discussion in 'Javascript' started by Fabian, Nov 7, 2003.

  1. Fabian

    Fabian Guest

    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, Nov 7, 2003
    #1
    1. Advertising

  2. Fabian

    Fabian Guest

    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
     
    Fabian, Nov 7, 2003
    #2
    1. Advertising

  3. Fabian

    Evertjan. Guest

    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.style.fontSize = "7pt"
    coll.style.color = "black"
    }
    }



    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
     
    Evertjan., Nov 7, 2003
    #3
  4. "Fabian" <> 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.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
    --
    Lasse Reichstein Nielsen -
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    'Faith without judgement merely degrades the spirit divine.'
     
    Lasse Reichstein Nielsen, Nov 7, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dean H. Saxe
    Replies:
    0
    Views:
    1,085
    Dean H. Saxe
    Jan 3, 2004
  2. Rob Nicholson
    Replies:
    3
    Views:
    842
    Rob Nicholson
    May 28, 2005
  3. Dave
    Replies:
    1
    Views:
    348
    Mike Wahler
    Jan 22, 2005
  4. Ken Fine
    Replies:
    0
    Views:
    302
    Ken Fine
    Jan 5, 2007
  5. DC
    Replies:
    2
    Views:
    665
Loading...

Share This Page