SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Wizard
    Join Date
    Oct 2005
    Location
    London
    Posts
    1,678
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    grabbing the value of a textnode

    Hi,

    I have this little snippet of code. I'm trying to get the text out of the option tag (not the value of the option but the actual text inbetween the option tags).
    Nodetype 3 is indeed the text node...but when I try to alert out the nodevalue it just alerts a blank alert box.



    var allSelects = document.getElementsByTagName('select');
    for(var i=0; i<allSelects.length; ++i) {
    var options = allSelects[i].childNodes;
    for(var m=0;m<options.length; m++) {
    if(options[m].nodeType == 3) {
    alert(options[m].nodeValue);
    }
    }
    }
    }


    any ideas?

  2. #2
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Up to IE7 the option element has no child nodes, only properties (value, text),
    though the other browsers treat it like any other element, with a textNode.

    IE8 (beta) handles options as nodes, but while older versions are out there you'll need to sniff around for the correct reference to an option's text.
    You can try the childNode of an option and catch a text property, or just look for a browser specific property for an option's text content:


    Code:
    function listOptionText(){
    	var allSelects= document.getElementsByTagName('select');
    	var O= [], temp, temp2, temp3, s, O2;
    	for(var i= 0, L= allSelects.length; i< L; ++i){
    		temp= allSelects[i].options;
    		O2= [];		
    		for(var i2= 0, L2= temp.length; i2< L2; i2++){
    			temp2= temp[i2];
    			s= temp2.text || temp2.textContent || temp2.innerText || '';
    			O2[O2.length]= s;			
    		}
    		O[O.length]= O2.join(', ');
    	}
    	return (O.join('\n'));
    }
    alert(listOptionText());

  3. #3
    SitePoint Wizard
    Join Date
    Oct 2005
    Location
    London
    Posts
    1,678
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    thanks for the reply,

    I've just tested this in ie6, 7 anf ff3 and it seems to work:

    var text = document.createTextNode(options[m].text);

    ?

  4. #4
    SitePoint Zealot Amenthes's Avatar
    Join Date
    Oct 2006
    Location
    Bucharest, Romania
    Posts
    143
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Wouldn't this do the job just as well?

    var text = document.createTextNode(options[m].innerHTML);
    I'm under construction | http://igstan.ro/

  5. #5
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,684
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    That only works when the page is served as HTML. As soon as it's served as XHTML, innerHTML won't work.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  6. #6
    SitePoint Zealot Amenthes's Avatar
    Join Date
    Oct 2006
    Location
    Bucharest, Romania
    Posts
    143
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by pmw57 View Post
    That only works when the page is served as HTML. As soon as it's served as XHTML, innerHTML won't work.
    You're absolutely right, but I really doubt he's using real XHTML.
    I'm under construction | http://igstan.ro/

  7. #7
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,684
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    IE users will be in real trouble when pages are given to them as XHTML, that's for sure.
    For the forseeable future, innerHTML can be an acceptable compromise.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  8. #8
    SitePoint Wizard
    Join Date
    Oct 2005
    Location
    London
    Posts
    1,678
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Amenthes View Post
    You're absolutely right, but I really doubt he's using real XHTML.
    I am using valid XHTML, validating against a transistional DOCTYPE.

    I'm sticking with the method I have using as it's working corss browser no problem

  9. #9
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,684
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    You're using XHTML but it's being served as html.
    If it's served as application/xhtml+xml then IE won't be able to handle it. IE can only work with XHTML files when they are served as HTML files, which means that any of the non HTML features of XHTML aren't capable of being performed.

    XHTML served as HTML. We might as well stay coding with HTML, which many people are.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


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
  •