JavaScript dynamic DOM retrieval Jan03 '07
Your dynamic page generates an HTML table from a database query. Each record has a unique ID, and you use that ID for the id attribute on the <tr> elements:
....
<tr id="myRow_13">
<td></td>
</tr>
<tr id="myRow_29">
<td></td>
</tr>
....
This provides each <tr> with a unique ID (id attributes must be unique, anyway), so you can now target specific table rows with JavaScript.
Problem is... how do you know which rows are pulled? In other words, when the page loads the dynamic content, JavaScript won't know which unique id's are actually residing in the DOM.
Proposed definition of "dynamic": could change.
Pull all <tr> elements
JavaScript typically targets specific elements using getElementById(). We won't be able to use this, because we don't know the id's on the page. Instead, let's use getElementsByTagName().
getElementsByTagName() does just what it sounds like - it gets all elements using a certain tag name.
var allTrElements = getElementsByTagName("tr");
This will grab all <tr> elements, and store them into an array, which we can then loop through to perform additional programming.
Problem solved?
Not quite. getElementsByTagName("tr") will pull all <tr> elements on the page, including ones you may not want or need access to.
So how do we pull only the <tr> elements from the specific table with the database rows?
Pull only certain <tr> elements
We can look at the id attribute, and base it off that. If the id attribute starts with "myRow" - then we know that's a <tr> we want to work with.
// STORE ALL <tr> ELEMENTS IN AN ARRAY.
var allTrElements = getElementsByTagName("tr");
// LOOP THROUGH ALL <tr> ELEMENTS ON THE PAGE.
for ( var i=0; i < allTrElements.length; i++ )
{
// SET THE CURRENT <tr> ELEMENT TO A VARIABLE.
var tr = allTrElements[i];
// IF THE CURRENT <tr> HAS AN id ATTRIBUTE
// THAT BEGINS WITH "myRow", THEN PROCEED.
if ( tr.id.substr( 0, 5 ) == "myRow" )
{
// EXTRACT THE id NUMBER OFF OF THE id ATTRIBUTE.
var rowId = tr.id.substr( 6 );
// TARGET THE SPECIFIC ROW.
var rowTarget = document.getElementById( "myRow_" + rowId );
// DO SOMETHING SPECIFIC TO THE ROW.
rowTarget.className = "highlight";
}
}
So, using the id attribute of the <tr> element, we can throw away the <tr> elements that we don't want to target, as long as each id is named consistently.
Categories: JavaScript
, Tutorials ![]()
Add Feedback (view all)
Leave feedback
matthom
is published and produced by Matt Thommes - an independent publishing enthusiast, mobile blogger, content creator, informative writer, web developer from a suburb of Chicago.
Never one to conform, Matt intends to promote the effect the web has on our lives, in an effort to intensify, instruct, and clarify all that is happening around us.
Popular Pages
- Fast rounded corners in Photoshop (7378 recent visits)
- PHP – passing variables across pages (2702 recent visits)
- JavaScript set selected on load (2271 recent visits)
- Removing all child nodes from an element (1648 recent visits)
- iPod songs out of order? (1304 recent visits)
- Firefox 3 smart address bar: wildcard search (1232 recent visits)
- Britney - Everytime piano tab (1109 recent visits)
- MySQL LEFT JOIN syntax (931 recent visits)
- Breathe Me - Sia (785 recent visits)
- Tumblr: how blogging should be (687 recent visits)
Similar Entries
- JavaScript badges should reside on the bottom of the HTML page (27 recent visits)
- JavaScript: billing/shipping address copy (28 recent visits)
- JavaScript string replace for post slug (327 recent visits)
- Using JavaScript to obtain data from HTML and XML (61 recent visits)
- JavaScript set selected on load (2271 recent visits)
- JavaScript can access hidden HTML (121 recent visits)
Stats
102 unique visits since August 2008
Recent Referrers (click)
- javascript loop all tr elements
- loop thru table elements javascript dom
- DOM dynamic Row form problem POST
- dynamic id javascript
- loop elements javascript dom
- javascript loop through dom
- javascript dynamic tr row
- javascript dynamic ids
- javascript dynamic ids
- loop through dynamic controls in Javascript
- loops for DOM + javascript + child
- loop DOM child element
- DOM javascript add "child elements" id
- javascript loop dom
- jscript looping through specific dom elements
- getElementsByTagName
- access html dynamic dom elements
- javascript element xml rowID