SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    Aug 2007
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    document.getElementById('td_1').content ?

    <table>
    <tr>
    <td id="id_1">
    Welcome
    </td>
    </tr>
    </table>


    how could i get "Welcome" string in javascript ?
    document.getElementById('id_1').content; // error
    document.getElementById('id_1').text; // error

    any help ?

  2. #2
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    An element can have text in multiple text nodes or nested child nodes-
    a div or td can contain a p with a span, for example.
    Either read the innerHTML of the parent element, or recursively read the child nodes.
    The first method is simpler to call(text= element.innerHTML), but may return html markup you don't want.

    The recursive method is more involved, but returns all of the text in all of the text nodes contained by any element.

    Code:
    function getText(hoo){
    	var what= [], next= hoo.firstChild, txt;
    	while(next){
    		if(next.nodeType== 3) txt= next.data;
    		else{
    			if(next.hasChildNodes()) txt= getText(next);
    			if(txt)txt=' '+txt;
    		}
    		if(txt) what.push(txt);
    		next= next.nextSibling;
    	}
    	return  what.join('');
    }
    alert(getText(document.getElementById('id_1')))

  3. #3
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    Because microsoft seem to have us stuck forever in HTML land with no hope of seeing XHTML from IE, why don't we just use innerHTML instead?

    This question is not in support of innerHTML, but to try and gain a community impression of the state of the field.

    document.getElementById('id_1').innerHTML;
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  4. #4
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,875
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    If you know it is only text then you could also use

    document.getElementById('id_1').firstChild.data


    If that part of the page was generated from JavaScript using DOM calls then it may not be accessible from innerHTML but if it is part of the original HTML then it will be.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  5. #5
    Guru in training bronze trophy SoulScratch's Avatar
    Join Date
    Apr 2006
    Location
    Maryland
    Posts
    1,838
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'd go with innerHTML on this one.


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
  •