F
Faisal Vali
Hi - I'm new to javascript and I was reading the book Javascript
Professional Projects - there is a fragment that has me a little
perplexed, and I was wondering if anyone could explain why and how it
works.
How is it that setting a property to null alters another property?
For example setting
optionObject.options[optionObject.selectedIndex] = null;
seems to set optionObject.selectedIndex to -1 automagically.
Can you attach "hooks" in javascript that get called whenever a
property is modified?
Or does an eventhandler get triggered behind the scenes that updates
selectedIndex - and if so - how do i attach similar eventhandlers to
objects - or is this an intrinsic feature of javascript?
Here is the function. I have inserted some debug messages which
illustrate that as soon as the property is set to null -
fromSelect.selectedIndex gets updated.
function swapSelects( fromSelect, toSelect )
{
var toSelect_Length = toSelect.options.length;
while( fromSelect.selectedIndex > -1 )
{
var index = fromSelect.selectedIndex;
alert( "1>" + fromSelect.selectedIndex );
toSelect.options[toSelect_Length] = new Option(
fromSelect.options[index].text );
toSelect.options[toSelect_Length].value =
fromSelect.options[index].value;
alert( "2>" + fromSelect.selectedIndex );
// note this is the line that triggers the update
fromSelect.options[index] = null;
alert( "3>" + fromSelect.selectedIndex );
toSelect_Length++;
}
thanks in advance.
Faisal Vali.
Professional Projects - there is a fragment that has me a little
perplexed, and I was wondering if anyone could explain why and how it
works.
How is it that setting a property to null alters another property?
For example setting
optionObject.options[optionObject.selectedIndex] = null;
seems to set optionObject.selectedIndex to -1 automagically.
Can you attach "hooks" in javascript that get called whenever a
property is modified?
Or does an eventhandler get triggered behind the scenes that updates
selectedIndex - and if so - how do i attach similar eventhandlers to
objects - or is this an intrinsic feature of javascript?
Here is the function. I have inserted some debug messages which
illustrate that as soon as the property is set to null -
fromSelect.selectedIndex gets updated.
function swapSelects( fromSelect, toSelect )
{
var toSelect_Length = toSelect.options.length;
while( fromSelect.selectedIndex > -1 )
{
var index = fromSelect.selectedIndex;
alert( "1>" + fromSelect.selectedIndex );
toSelect.options[toSelect_Length] = new Option(
fromSelect.options[index].text );
toSelect.options[toSelect_Length].value =
fromSelect.options[index].value;
alert( "2>" + fromSelect.selectedIndex );
// note this is the line that triggers the update
fromSelect.options[index] = null;
alert( "3>" + fromSelect.selectedIndex );
toSelect_Length++;
}
thanks in advance.
Faisal Vali.