15.02.2013 Views

JavaScript Examples Bible - UserWorks Technologies

JavaScript Examples Bible - UserWorks Technologies

JavaScript Examples Bible - UserWorks Technologies

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

uniqueID<br />

Chapter 1 ✦ Generic HTML Element Objects (Chapter 15)<br />

NN2 NN3 NN4 NN6 IE3/J1 IE3/J2 IE4 IE5 IE5.5<br />

Compatibility ✓ ✓<br />

Example<br />

Listing 15-18 demonstrates the recommended syntax for obtaining and applying<br />

a browser-generated identifier for an object. After you enter some text into the text<br />

box and click the button, the addRow() function appends a row to the table. The<br />

left column displays the identifier generated via the table row object’s uniqueID<br />

property. IE5+ generates identifiers in the format “ms__idn”, where n is an integer<br />

starting with zero for the current browser session. Because the addRow() function<br />

assigns uniqueID values to the row and the cells in each row, the integer for each<br />

row is three greater than the previous one. There is no guarantee that future generations<br />

of the browser will follow this format, so do not rely on the format or<br />

sequence in your scripts.<br />

Listing 15-18: Using the uniqueID Property<br />

<br />

<br />

Inserting an IE5+/Windows Table Row<br />

<br />

function addRow(item1) {<br />

if (item1) {<br />

// assign long reference to shorter var name<br />

var theTable = document.all.myTable<br />

// append new row to the end of the table<br />

var newRow = theTable.insertRow(theTable.rows.length)<br />

// give the row its own ID<br />

newRow.id = newRow.uniqueID<br />

// declare cell variable<br />

var newCell<br />

// an inserted row has no cells, so insert the cells<br />

newCell = newRow.insertCell(0)<br />

// give this cell its own id<br />

newCell.id = newCell.uniqueID<br />

// display the row’s id as the cell text<br />

newCell.innerText = newRow.id<br />

newCell.bgColor = “yellow”<br />

// reuse cell var for second cell insertion<br />

newCell = newRow.insertCell(1)<br />

newCell.id = newCell.uniqueID<br />

Continued<br />

49<br />

elementObject.uniqueID

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!