SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Hybrid View

  1. #1
    SitePoint Member
    Join Date
    Sep 2004
    Location
    antwerp
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Unidentified only in IE

    I have a problem that's only occuring in IE.

    Code:
    curName = curlabel.firstChild.nodeValue;
    is returning unidentified while all others return the correct value. The idea is to extract Street address from the label.

    Code:
    <label for="street">Street address</label>
    Has anyone run into this sort of thing before?

  2. #2
    SitePoint Wizard gRoberts's Avatar
    Join Date
    Oct 2004
    Location
    Birtley, UK
    Posts
    2,439
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Unidentified? Undefined!

    why don't you just use innerText? When you say its returning undefined for the street label yet works fine on others, could you provide a sample that replicates this issue that we can see?


  3. #3
    SitePoint Member
    Join Date
    Sep 2004
    Location
    antwerp
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    	function getLabelName(elementId){
    		var labels = document.getElementsByTagName('label');
    		for (var j = 0; j<labels.length ; j++)
    		{
    			var curlabel = labels[j];
    			var curName = curlabel.getAttribute('for');
    			if(curName == elementId)
    			{
    				curName = curlabel.firstChild.nodeValue;
    				return curName;
    			}
    		}
    	}
    I hope this makes it a bit more clear.

  4. #4
    SitePoint Wizard gRoberts's Avatar
    Join Date
    Oct 2004
    Location
    Birtley, UK
    Posts
    2,439
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you shouldn't be able to get the For attribute using getAttribute. Instead you have to use htmlFor.

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
          "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
        <head>
            <title>Untitled Document</title>
            <link rel="stylesheet" type="text/css" media="screen" />
            <style type="text/css"></style>
            <script type="text/javascript">
                function getLabelName(elementId) {
                    var labels = document.getElementsByTagName('LABEL');
                    for(var i = 0; i < labels.length; i++) {
                        var getAtt = labels[i].htmlFor;
                        if(getAtt == elementId) {
                            if(labels[i].firstChild)
                                return labels[i].firstChild.nodeValue;
                            else
                                return labels[i].innerText;
                        }
                    }
                }
            </script>
        </head>
        <body>
            
            <label for="test">Test 1:</label><input type="text" id="test" /><br />
            <label for="test2">Test 2:</label><input type="text" id="test2" /><br />
            <label for="test3">Test 3:</label><input type="text" id="test3" /><br />
            <br />
            <input type="button" onclick="getLabelName('test');" value="test" />
    
        </body>
    </html>


  5. #5
    SitePoint Member
    Join Date
    Sep 2004
    Location
    antwerp
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That did the trick,

    thank you


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
  •