Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

230
Views
Why do I get 'this.XXXXX is not a function' when it is a function?

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?

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!