#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2016
    Location
    Lakewood, WA
    Posts
    82
    Rep Power
    17

    Manipulating text in a SPAN tag


    I have some text is a <span> tag:

    Code:
    <span id="my_text">Some text...</span>
    Depending on certain actions, I'd like that text to go away.

    This does not work:

    Code:
    document.getElementById('my_text').value = "";
    Is there a way to do this?
  2. #2
  3. Forgotten Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    15,977
    Rep Power
    9615
    SPANs don't have values (they're not form fields) but they do have innerHTML content...
  4. #3
  5. ~ bald headed old fart ~
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2005
    Location
    chertsey, a small town s.w. of london, england
    Posts
    347
    Rep Power
    90
    Hi there Arty Zifferelli


    you could also use...

    Code:
    
    document.getElementById('my_text').textContent='';

    Differences from innerHTML

    innerHTML returns the HTML as its name indicates. Quite often,
    in order to retrieve or write text within an element, people
    use innerHTML. textContent should be used instead. Because
    the text is not parsed as HTML, it's likely to have better
    performance. Moreover, this avoids an XSS attack vector.

    Source:-


    coothead
    ~ the original bald headed old fart ~

IMN logo majestic logo threadwatch logo seochat tools logo