18.04.2016 Views

Professional JavaScript For Web Developers

javascript for learners.

javascript for learners.

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.

<strong>For</strong>ms and Data Integrity<br />

This method removes all options by iterating in reverse order. This is necessary because every time an<br />

option is removed, the index property of each option is reset to the proper position. <strong>For</strong> this reason, it is<br />

always best to remove the option with the highest index first and work your way back to the option with<br />

the lowest index. Otherwise, you must keep track of multiple changing indexes.<br />

Moving Options<br />

In early <strong>JavaScript</strong>, moving options from one list box to another was a rather arduous process that<br />

involved removing the option from the first list box, creating a new option with the same name and value,<br />

then adding that new option to the second list box. <strong>For</strong>tunately, the DOM provides a much more concise<br />

way of doing things. Using DOM methods, it’s possible to literally move an option from the first list box<br />

into the second list box by using the appendChild() method. If you pass an element that is already in the<br />

document into this method, the element is removed from its parent and put into the position specified.<br />

The method to execute this functionality accepts three arguments: the list box that the option currently<br />

resides in, the list box to move the option to, and the index of the option to move. The method can then<br />

take the option in the given index (assuming it exists) and move it to the second list box:<br />

ListUtil.move = function (oListboxFrom, oListboxTo, iIndex) {<br />

var oOption = oListboxFrom.options[iIndex];<br />

}<br />

if (oOption != null) {<br />

oListboxTo.appendChild(oOption);<br />

}<br />

It is then possible to move a given option from one list box to another by using code such as the<br />

following:<br />

var oListbox1 = document.getElementById(“selListbox1”);<br />

var oListbox2 = document.getElementById(“selListbox2”);<br />

ListUtil.move(oListbox1, oListbox2, 0); //move the first option<br />

This code moves the first option from oListbox1 into oListbox2 (the new option appears at the<br />

bottom of oListbox2).<br />

Moving options is the same as removing them in that the index property of each<br />

option is reset into the proper position, so you should always move the option with<br />

the highest index first.<br />

Reordering options<br />

To reorder options in a list box, moving a particular option either up or down, two methods are necessary,<br />

one to shift an option up and one to shift an option down. Each method takes two arguments: the<br />

list box to act on and the index of the option to move. Both also make use of the DOM insertBefore()<br />

method to reorder the elements.<br />

361

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

Saved successfully!

Ooh no, something went wrong!