Thursday, October 4, 2007

JavaScript : Attach event handler to control at runtime

Here is the sample.


 


 


 

var RadioButton1 = document.getElementById(LogoSettingId).children[0].children[0].children[0].children[0];


 


if (RadioButton1.addEventListener)

{


// Non-IE browsers

RadioButton1.addEventListener('onclick', RadioButton1_click, false);        

}


else
if (RadioButton1.attachEvent)

{


// IE 6+

RadioButton1.attachEvent('onclick', RadioButton1_click);

}


else

{


// Older browsers


var currentEventHandler = RadioButton1['onclick'];


if (currentEventHandler == null)

{

RadioButton1['onclick'] = RadioButton1_click;

}


else

{

RadioButton1['onclick'] = function(e)

{

currentEventHandler(e); RadioButton1(e);

}

}

}