SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Hybrid View

  1. #1
    SitePoint Addict SwordsmanX's Avatar
    Join Date
    Sep 2005
    Posts
    211
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Tricky DOM question for you :D

    I have this:

    <div id="cont">This is some <b>text</b></div>

    How can I get "This is some" (without the <b> tag)?
    innerHTML won't cut it because it includes the <b> tag, so could you suggest a solution?

  2. #2
    do righteous deeds
    Join Date
    Aug 2006
    Location
    Türkiye Turkey تركيى
    Posts
    266
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
      
    <script type="text/javascript">
    
    function getText() {
    var el = document.getElementById('cont');
    alert(el.firstChild.data); // This is some
    }
    </script>
    <input type="button" onclick="getText()">
    
    
    <div id="cont">This is some <b>text</b></div>
    Code:
     
    <script type="text/javascript">
    
    function getText() {
    var el = document.getElementById('cont');
    var n = el.innerHTML;
    alert(n);  // This is some <b>text</b>
    n = n.replace("<b>","");
    n = n.replace("</b>","");
    alert(n);  // This is some text
    }
    </script>
    <input type="button" onclick="getText()">
    
    
    <div id="cont">This is some <b>text</b></div>

  3. #3
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    muazzez, he said he only wants "This is some". Based on this, it means he doesn't want <b>text</b> at all.

    Code:
    document.getElementById('cont').firstChild
    The first child of 'cont' is a text node with the value 'This is some '.
    The second child is the <b> element.
    The <b> element contains a single child, which is a text node
    with the value 'text'.

  4. #4
    SitePoint Addict SwordsmanX's Avatar
    Join Date
    Sep 2005
    Posts
    211
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks, I have now access to the node but when I try to print it I don't get the text?
    let's say I want to substitute the text in another div with "This is some"
    Using document.getElementById('cont').firstChild won't make it (the div text becomes "[text] Object" or something similar)
    I tried using document.getElementById('cont').firstChild.value and document.getElementById('cont').firstChild.text but I get "undefined" for both...sorry for my newbishness :P

  5. #5
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Oh sorry, you need to do this:
    Code:
    document.getElementById('cont').firstChild.nodeValue

  6. #6
    SitePoint Addict SwordsmanX's Avatar
    Join Date
    Sep 2005
    Posts
    211
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thank you very much


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •