Change Label.text

Discussion in 'JavaScript' started by circuscircus, Jan 28, 2007.

Thread Status:
Not open for further replies.
  1. #1
    Basically I want to do this in Javascript:

    onclick {
    document.getElementById('label1').text = "Hello World";
    }

    <label id="label1"></label>

    (pseudocode ^)
     
    circuscircus, Jan 28, 2007 IP
  2. picouli

    picouli Peon

    Messages:
    760
    Likes Received:
    89
    Best Answers:
    0
    Trophy Points:
    0
    #2
    try innerHTML
    document.getElementById('label1').innerHTML = "Hello World";

    HTH (I'm not 100% sure, thou...)
     
    picouli, Jan 29, 2007 IP
  3. datropics

    datropics Peon

    Messages:
    309
    Likes Received:
    3
    Best Answers:
    1
    Trophy Points:
    0
    #3
    Hi CircusCircus,

    piCouli is correct - the advantage with his/her approach is that you can add formatting to the string like "<b>Hello World</b>";. With what you were doing originally, the correct statement would be document.getElementById('label1').innerText = "Hello World";. However the disadvantage of your original approach is that you CANNOT add formatting.

    HTH
     
    datropics, Jan 29, 2007 IP
  4. Logic Ali

    Logic Ali Well-Known Member

    Messages:
    170
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    108
    #4
    That cannot be a correct statement, because .innerText is I.E. only.
    <LABEL id="label1" style="font-weight:bold;color:#f20"></LABEL><br>
    
    <A HREF='#' onclick="return setText('label1','Hello whirled');">CLICK</A>
    
    <SCRIPT type='text/javascript'>
    
    function setText(id, txt)
    {
     var elem;
       
     if( document.getElementById  && (elem=document.getElementById(id)) )
     {
      if( !elem.firstChild )
       elem.appendChild( document.createTextNode( txt ) );
      else 
       elem.firstChild.data = txt;
     }
    
     return false; 
    }
    </SCRIPT>
    Code (markup):
     
    Logic Ali, Jan 29, 2007 IP
  5. datropics

    datropics Peon

    Messages:
    309
    Likes Received:
    3
    Best Answers:
    1
    Trophy Points:
    0
    #5
    Logic Ali is correct - good for you Ali!
     
    datropics, Jan 29, 2007 IP
Thread Status:
Not open for further replies.