Change CSS Class property using javascript?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Noozer

    Change CSS Class property using javascript?

    I have several tags on a webpage of the same class. If the user clicks a
    specific checkbox I'd like to be able to alter the display property of the
    class, affecting all objects of that class.

    This is an intranet application so we know that javascript will be enabled
    and the browser will be IE.

    How can I affect all the members of this class? Is there a way I can toggle
    the DISPLAY property of a class so all the elements using that class would
    be affected?

    ie:

    default.css file contains the following and is included into the HTLM
    document:

    .topicone { display:none; }
    .topictwo { display:none; }

    Relevant section of HTML code:
    <input type="checkbox" onclick="flip(' topicone')'">Sh ow me information
    about topic one.<br>
    <input type="checkbox" onclick="flip(' topictwo')'">Sh ow me information
    about topic two.<br>
    <p>Please see the following:</p>
    <p> Quick Points </p>
    <p class="topicone ">This is some information about topic one.</p>
    <p class="topictwo ">This is some information about topic two.</p>
    <p> Details </p>
    <p class="topicone ">More information about topic one.</p>
    <p class="topicone ">More information about topic one.</p>
    <p class="topictwo ">This is some information about topic two.</p>




  • RobB

    #2
    Re: Change CSS Class property using javascript?

    "Noozer" <dont.spam@me.h ere> wrote in message news:<k2Msd.434 866$nl.289708@p d7tw3no>...[color=blue]
    > I have several tags on a webpage of the same class. If the user clicks a
    > specific checkbox I'd like to be able to alter the display property of the
    > class, affecting all objects of that class.
    >
    > This is an intranet application so we know that javascript will be enabled
    > and the browser will be IE.
    >
    > How can I affect all the members of this class? Is there a way I can toggle
    > the DISPLAY property of a class so all the elements using that class would
    > be affected?
    >
    > ie:
    >
    > default.css file contains the following and is included into the HTLM
    > document:
    >
    > .topicone { display:none; }
    > .topictwo { display:none; }
    >
    > Relevant section of HTML code:
    > <input type="checkbox" onclick="flip(' topicone')'">Sh ow me information
    > about topic one.<br>
    > <input type="checkbox" onclick="flip(' topictwo')'">Sh ow me information
    > about topic two.<br>
    > <p>Please see the following:</p>
    > <p> Quick Points </p>
    > <p class="topicone ">This is some information about topic one.</p>
    > <p class="topictwo ">This is some information about topic two.</p>
    > <p> Details </p>
    > <p class="topicone ">More information about topic one.</p>
    > <p class="topicone ">More information about topic one.</p>
    > <p class="topictwo ">This is some information about topic two.</p>[/color]

    Not really enough information, but...IE-only:

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">

    <html>
    <head>
    <title>untitled </title>
    <style type="text/css">

    body {
    font: normal 90% verdana;
    }
    ..topicone {
    font: normal 80% verdana;
    color: #600;
    display: none;
    }
    ..topictwo {
    font: normal 80% verdana;
    color: #060;
    display: none;
    }

    </style>
    <script type="text/javascript">

    function IE_set_class(cl anSheetame)
    {
    var re = new RegExp(clanShee tame);
    var rules, nRule,
    nRules, SS, nSheet = 0,
    nSheets = document.styleS heets.length;
    for (nSheet; nSheet < nSheets; ++nSheet)
    {
    SS = document.styleS heets.item(nShe et);
    rules = SS.rules;
    nRules = rules.length;
    for (nRule = 0; nRule < nRules; ++nRule)
    {
    rule = SS.rules.item(n Rule);
    if (re.test(rule.s electorText))
    {
    for (var a = 1; a < arguments.lengt h; a+=2)
    rule.style[arguments[a]] = arguments[a + 1];
    return;
    }
    }
    }
    }

    function toggleclass(obj )
    {
    var dval = obj.checked ? 'block' : 'none';
    IE_set_class(ob j.value, 'display', dval)
    }

    onload = function()
    {//keep things in sync
    var i = 0,
    ipt,
    ipts = document.getEle mentsByTagName( 'input');
    while (ipt = ipts.item(i++))
    if (/toggle/i.test(ipt.name ))
    ipt.onclick();
    }

    </script>
    </head>
    <body>
    <form>
    <input type="checkbox" name="toggle" value="topicone "
    onclick="toggle class(this)">Sh ow me information
    about topic one.<br>
    <input type="checkbox" name="toggle" value="topictwo "
    onclick="toggle class(this)">Sh ow me information
    about topic two.<br>
    <p>Please see the following:</p>
    <p> Quick Points </p>
    <p class="topicone ">This is some information about topic one.</p>
    <p class="topictwo ">This is some information about topic two.</p>
    <p> Details </p>
    <p class="topicone ">More information about topic one.</p>
    <p class="topicone ">More information about topic one.</p>
    <p class="topictwo ">This is some information about topic two.</p>
    </form>
    </body>
    </html>

    Comment

    • Rob B

      #3
      Re: Change CSS Class property using javascript?



      ...lol - I did a S&R to change var 'SSn' to 'nSheet' and it also changed
      'classname' to 'clanSheetame' which sounds vaguely Scottish. I am sorry.
      :o

      *** Sent via Developersdex http://www.developersdex.com ***
      Don't just participate in USENET...get rewarded for it!

      Comment

      • RobG

        #4
        Re: Change CSS Class property using javascript?

        Rob B wrote:[color=blue]
        >
        > ...lol - I did a S&R to change var 'SSn' to 'nSheet' and it also changed
        > 'classname' to 'clanSheetame' which sounds vaguely Scottish. I am sorry.
        > :o[/color]

        And here I was thinking you were being creative. Shazam!

        --
        Rob

        Comment

        Working...