15.02.2013 Views

JavaScript Examples Bible - UserWorks Technologies

JavaScript Examples Bible - UserWorks Technologies

JavaScript Examples Bible - UserWorks Technologies

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

78 <strong>JavaScript</strong> <strong>Examples</strong> <strong>Bible</strong>: The Essential Companion to <strong>JavaScript</strong> <strong>Bible</strong><br />

item(index | “index” [, subIndex])<br />

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

Compatibility ✓ ✓ ✓ ✓<br />

Example<br />

Use The Evaluator (Chapter 13 in the <strong>JavaScript</strong> <strong>Bible</strong>) to experiment with the<br />

item() method. Type the following statements into the topmost text box and view<br />

the results for each:<br />

NN6 and IE5<br />

document.getElementById(“myP”).childNodes.length<br />

document.getElementById(“myP”).childNodes.item(0).data<br />

document.getElementById(“myP”).childNodes.item(1).nodeName<br />

NN6, IE4, and IE5<br />

document.forms[1].elements.item(0).type<br />

IE4 and IE5<br />

document.all.item(“myP”).outerHTML<br />

myP.outerHTML<br />

In the last two examples, both statements return the same string. The first example<br />

is helpful when your script is working with a string version of an object’s name.<br />

If your script already knows the object reference, then the second approach is more<br />

efficient and compact.<br />

mergeAttributes(“sourceObject”)<br />

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

Compatibility ✓ ✓<br />

Example<br />

Listing 15-29 demonstrates the usage of mergeAttributes() in the process of<br />

replicating the same form input field while assigning a unique ID to each new field.<br />

So you can see the results as you go, I display the HTML for each input field in the<br />

field.<br />

The doMerge() function begins by generating two new elements: a P and an<br />

INPUT element. Because these newly created elements have no properties associated<br />

with them, a unique ID is assigned to the INPUT element via the uniqueID<br />

property. Attributes from the field in the source code (field1) are merged into the<br />

new INPUT element. Thus, all attributes except name and id are copied to the new<br />

element. The INPUT element is inserted into the P element, and the P element is<br />

elementObject.mergeAttributes()

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

Saved successfully!

Ooh no, something went wrong!