Jump to content

Trying Access Node Using Js And Dom


LogiQ121

Recommended Posts

I am trying to access the Text Node "498073" of the table below using Javascript and DOM. All my attempts at getting the nodeValue of that node has failed. Here is what I have tried to get the value of the Node:

z=document.getElementById("SubsList").lastChild.firstChild.childNodes[1].childNodes[1].nodeValue;

<html><html><body><div id="SubsList">    <strong>Customer Subscriptions</strong>    <div>	<table cellspacing="0" rules="all" border="1" id="ctl00_Container_SubsGrid" style="border-collapse:collapse;">		<tr class="ghead">			<th scope="col"> </th><th scope="col">Sub #</th><th scope="col">Product</th><th scope="col">Order Price</th><th scope="col">Shipping Price</th><th scope="col">Bill Interval</th><th scope="col">Bill Cycles</th><th scope="col">Ship Product?</th><th scope="col">Next Bill Date</th><th scope="col">Status</th>		</tr><tr class="grow">			<td><a href="java script:__doPostBack('ctl00$Container$SubsGrid','Edit$0')">Edit</a></td><td>498073</td><td>ACPUR-00</td><td>$39.95</td><td>$0.00</td><td>0</td><td>1</td><td>0</td><td>10/29/2008</td><td>INACTIVE</td>		</tr><tr class="galt">			<td><a href="java script:__doPostBack('ctl00$Container$SubsGrid','Edit$1')">Edit</a></td><td>608539</td><td>ACPUR-01A</td><td>$39.95</td><td>$4.95</td><td>30</td><td>0</td><td>1</td><td>03/14/2009</td><td>ACTIVE</td>		</tr>	</table>    </div>    <a href="Addsubscription.aspx?cid=428697">Add Subscription</a></div></body></html>

What am I doing wrong? Any help would be much appreciated.

Link to comment
Share on other sites

For starters, you're looking for the lastChild of the div called "SubsList". The last element of that div is a link. The lastChild of that div is the textNode that comes after the link. That text node holds a linebreak. And it doesn't have any children. There's nothing else there.What you want is in the table, several nodes up the tree. How many nodes? Depends on the browser.If you want to get a node, start by referencing the nearest ancestor you can get a handle on. You started with "SubsList". But the table has an id, "ctl00_Container_SubsGrid". So why not start there?But don't go looking directly at children. They include textNodes, as we just saw. They throw off the whole count, and different browsers count them differently. This will get you much closer:var rows = document.getElementById("ctl00_Container_SubsGrid").rows;Now you have a list of rows. rows[0] holds all the headers. rows[1] holds the table cell you want. rows[1].cells is a list of cells. rows[1].cells[1] holds the textNode you want. So one answer to the question is this:document.getElementById("ctl00_Container_SubsGrid").rows[1].cells[1].firstChild.nodeValueordocument.getElementById("ctl00_Container_SubsGrid").rows[1].cells[1].innerHTMLThe next big question is how much of this information can you hard-code, and how much do you need to discover dynamically. Like, is there always going to be just one table? If there are more, then maybe you do need to start with "SubsList" and go looking for tables. Will you always want the second cell of the second row?Answers to those questions will lead you to the best tree, or to an algorithm if you need to go hunting.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...