I have the below code. All I want to do is do different work based on the radio button value selected, and I am using onclick.
/* HTML */
<input label='radio' id='radioYes' name='selectionA' value='yes'>
<input label='radio' id='radioNo' name='selectionA' value='no'>
/* JavaScript */
(function(){
var obj = {
start: function(){
/* Do some work */
this.addEvents();
/* Do some work */
},
addEvents: function(){
document.getElementById('radioYes').onclick = function() {this.radioClicked(true);};
document.getElementById('radioNo').onclick = function() {this.radioClicked(false);};
},
radioClicked: function(bool){
if(bool){
/*Yes was selected. Do some work.*/
}
else{
/*No was selected. Do some work.*/
}
}
};
obj.start();
}();
When I click on the radio button, I get TypeError: this.radioClicked is not a function
but I can't figure out why. The function 'radioClicked' is defined right below!
When I do the following without any parameters, it works fine.
addEvents: function(){
document.getElementById('radioYes').onclick = this.radioClicked;
document.getElementById('radioNo').onclick = this.radioClicked;
},
radioClicked: function(){
/* Do some work */
}
I read on some website that if you want to pass parameters, you can put your function inside an anonymous function like I did in the first code that's not working. Could someone please help me what I am doing here?