Change all label appearances

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

    #1

    Change all label appearances

    Hi there,

    In my form, after validation, labels of wrong
    fields are turned into a red color.
    What i want now is when you hit 'reset', to make all
    the labels grey again.

    I have the next code:
    =============== =============== ========
    function GrayLabels()
    {
    var label;
    label = document.getEle mentById(label) ;
    if (label && label.style) {
    label.style.col or = '#A2A2A2';
    };
    =============== =============== =========

    So reset will have onClick="GrayLa bels()" in it. But how
    do i automatically apply this to everey label in the document?

    Hope this is clear enough and thanks in advance..

    Greetings
  • Grant Wagner

    #2
    Re: Change all label appearances

    knoak wrote:
    [color=blue]
    > Hi there,
    >
    > In my form, after validation, labels of wrong
    > fields are turned into a red color.
    > What i want now is when you hit 'reset', to make all
    > the labels grey again.
    >
    > I have the next code:
    > =============== =============== ========
    > function GrayLabels()
    > {
    > var label;
    > label = document.getEle mentById(label) ;
    > if (label && label.style) {
    > label.style.col or = '#A2A2A2';
    > };
    > =============== =============== =========
    >
    > So reset will have onClick="GrayLa bels()" in it. But how
    > do i automatically apply this to everey label in the document?
    >
    > Hope this is clear enough and thanks in advance..
    >
    > Greetings[/color]

    Assuming you truly mean _every_ label in the document, you could
    use:

    <script type="text/javascript">
    function setLabelColor(l abelColor) {
    if (document.getEl ementsByTagName ) {
    var labels = document.getEle mentsByTagName( 'label');
    if (labels) {
    for (var i = 0; i < labels.length; i++) {
    if (labels[i] && labels[i].style) {
    labels[i].style.color = labelColor;
    }
    }
    }
    }
    }
    </script>
    <label id="one">ONE</label>
    <label id="two">TWO</label>
    <label id="three">THRE E</label>
    <br>
    <button onclick="setLab elColor('Gray') ;">Make Gray</button>
    <button onclick="setLab elColor('Black' );">Make Black</button>

    If you only want specific labels, you could set their ID to a
    specific prefix, then retrieve all labels and ignore those without
    the prefix:

    if (labels[i] && labels[i].style &&
    labels[i].id.indexOf('yo urPrefix') == 0) {

    labels[i].style.color = labelColor;
    }

    --
    | Grant Wagner <gwagner@agrico reunited.com>

    * Client-side Javascript and Netscape 4 DOM Reference available
    at:
    *


    * Internet Explorer DOM Reference available at:
    *
    http://msdn.microsoft.com/workshop/a...ence_entry.asp

    * Netscape 6/7 DOM Reference available at:
    * http://www.mozilla.org/docs/dom/domref/
    * Tips for upgrading JavaScript for Netscape 7 / Mozilla
    * http://www.mozilla.org/docs/web-deve...upgrade_2.html


    Comment

    • Thomas 'PointedEars' Lahn

      #3
      Re: Change all label appearances

      knoak wrote:[color=blue]
      > In my form, after validation, labels of wrong
      > fields are turned into a red color.
      > What i want now is when you hit 'reset', to make all
      > the labels grey again.[/color]

      You need to retrieve the collection of all LABEL elements.

      W3C DOM:
      document.getEle mentsByTagName( "label")

      IE4 DOM:
      document.all.ta gs("label")

      NS4 DOM:
      document.layers[number] if the elements are positioned

      Then iterate through the collection. Assign the empty string ("") to
      the respective CSS property to assign the initial value after all CSS
      rules have been rendered:

      var coll = null;

      if (document.getEl ementsByTagName ) // W3C DOM
      {
      coll = document.getEle mentsByTagName( "label");
      }
      else if (document.all && document.all.ta gs) // IE4 DOM
      {
      coll = document.all.ta gs("label");
      }

      if (coll && coll.length)
      {
      for (var i = 0, len = collection.leng th; i < len; i++)
      {
      if ((o = collection[i].style) && typeof o.color != undefined)
      {
      o.color = "";
      }
      }
      }


      PointedEars

      Comment

      Working...