View Full Version : js in <option>
toe_head2001
12-25-2005, 11:24 PM
How do I execute js <option>, such as alert('Bla Bla Bla') ? I tried onselect, onmouseover, onclick, ect.
<select>
<option>Background Image</option>
<option value="3">Bla Bla</option>
<option value="2>Bla Bla</option>
<option value="5">Bla Bla</option>
</select>
thanks
jscheuer1
12-26-2005, 04:03 AM
<select onchange="alert(options[selectedIndex].text);">
<option>Background Image</option>
<option value="3">Bla Bla</option>
<option value="2>Bla Bla</option>
<option value="5">Bla Bla</option>
</select>
or
<select onchange="alert(options[selectedIndex].value);">
<option>Background Image</option>
<option value="3">Bla Bla</option>
<option value="2>Bla Bla</option>
<option value="5">Bla Bla</option>
</select>
Depending upon what you are looking for.
mwinter
12-26-2005, 03:31 PM
<select onchange="alert(options[selectedIndex].text);">You're assuming that the select element is in the scope chain of that event listener. Don't. Use the this operator:
<select onchange="alert(this.options[this.selectedIndex].text);">
Mike
jscheuer1
12-26-2005, 05:04 PM
You're assuming that the select element is in the scope chain of that event listener.
Why wouldn't it be? It is the select element's event. My first inclination was to use the this operator but, it appeared unnecessary. Anyways, tested fine in several browsers.
mwinter
12-26-2005, 06:16 PM
Why wouldn't it be? It is the select element's event.That's irrelevant. The scope chain is determined by the execution context when a function object is determined. As listeners created using intrinsic event attributes are internal, this environment can, and does, differ between implementations.
My first inclination was to use the this operator but, it appeared unnecessary.Appearances can be deceiving.
Anyways, tested fine in several browsers.All that proves is that custom scope chains are common among the browsers you tested, and I'm not disputing that. To be honest, I haven't personally encountered an exception either, but I don't have access to a wide variety of browsers. However, I do have anectotal evidence from another source that has seen this assumption fail.
Custom scope chains differ from being non-existent, to including the element, the element and the form, and even to including all DOM objects between the element and the global object. Given this variety, it is simpler to be aware of the existence (as it can adversely impact code in some instances), but to write code that ignores it. After all, given a choice between something that does work, and something that only might, which is the most logical? Especially when the difference between them is only five characters.
Mike
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.