Jump to content

Var value not working to get table row by Id


Recommended Posts

I'm trying to fill a table on my HTML page with values from my xml data file.

Var v is an HTMLCollection object containing these nodes extracted from the xml file:

<pubisher>Peters</pubisher>
<translator>Jay Reeve</translator>
<slidecount>885</slidecount>
<pagecount>189</pagecount>
<other></other>

I'm using this chunk of code. The vars are all local and declared earlier. 

for (r=0;r<v.length;r++) {
	str=v[r].innerHTML;				//str gets the value "Peters"
	if (str>"") {
		fld=v[r].nodeName;			//fld gets the value "publisher"
		x=document.getElementById(fld);		//x is "undefined"
		x.style.display="block";
		x.childNodes[1].innerText=str;
	}
}

On the first pass, x is supposed to reference the first row from this HTML table:

<table id="details">
   <tr id="publisher"> <td>  Publisher:</td><td></td></tr>
   <tr id="translator"><td> Translator:</td><td></td></tr>
   <tr id="slidecount"><td>Slide count:</td><td></td></tr>
   <tr id="pagecount"> <td> Page count:</td><td></td></tr>
   <tr id="other">     <td>      Other:</td><td></td></tr>
</table>

The debugger shows these values as they should be:
fld: "publisher"
str: "Peters"

but x=document.getElementById(fld) is left undefined and throws an error.

If I replace the variable fld  with the string "publisher", it works fine. The table row becomes visible and the empty cell gets the value "Peters".

So is there a restriction I don't know on using variables for Id's? Or Id's for table rows? I'm pretty sure I've done it before, but I can't see why this is failing. Is fld a restricted keyword? Could there be a type issue? I tried splicing quotes onto the fld string, but that didn't help. I don't think it can have anything to do with the fact that this is part of an anonymous callback function that runs after the file is loaded where v gets its value. Any insight would be most appreciated.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...