I have a composite control which emits HTML that looks like this
<span id="myControl">
<select id="myControl_select">
<option value=""></option>
<option value="1">Item 1</option>
<option value="2">Item 2</option>
</select>
</span>
I would like the parent span control to fire an event when the contained drop down list onchange event fires. Since this is a composite control, I have to allow a consumer of this control to be able to assign the event handler (I was hoping via an html attribute called onvaluechanged or something)
Editted to incorporate bobince's suggestion. The only problem is the argument parameter is not being passed. It's currently 'undefined'.
<script>
window.onload = function() {
var objSelect = document.getElementById('myControl_select')
if (objSelect.attachEvent)
objSelect.attachEvent("onchange", myControl_ddlOnChange);
else if (objSelect.addEventListener)
objSelect.addEventListener("onchange", myControl_ddlOnChange, false);
}
function myControl_ddlOnChange()
{
//fire span on value changed event
var target= document.getElementById('mycontrol');
var handler= target.getAttribute('onvaluechanged');
var args = { text : 'Hi!', index : 0 }
if (handler)
new Function(handler).call(target, args);
}
function myControl_ValueChanged()
{
alert(arguments[0]);
}
</script>
<span id="myControl" onvaluechanged="myControl_ValueChanged();">
<select id="myControl_select">
<option value=""></option>
<option value="1">Item 1</option>
<option value="2">Item 2</option>
</select>
</span>