Click here to Skip to main content
15,511,274 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
THIS IS MY HTML CODE:-
HTML
<button type="button" onclick="printAllRestaurantByName()">printAllRestaurantByName</button><br></br>


THIS IS MY JAVASCRIPT CODE:-
<pre lang="Javascript"><pre> class restaurantManager{
       restaurantList = [
        {name:"Barbeque Nation",address:"varanasi",city:"varansi"},
        {name:"The Table",address:"varnasi",city:"varansi"},
        {name:"i love my wife",address:"varansi",city:"varansi"},
        {name:"Batti chokha",address:"varansi",city:"varansi"}
       ]
   
    
 
   printAllRestaurantByName = () =>{
    for(let i=0;i>this.restaurantList.length;i++){
    console.log(this.restaurantList[i].name)}}}}


AFTER EXECUTING THIS I AM GETTING THE FOLLOWING ERRROR:

Uncaught ReferenceError: printAllRestaurantByName is not defined

at HTMLButtonElement.onclick

GUYS PLEASE HELP!!!!!!

What I have tried:

class restaurantManager{
      restaurantList = [
       {name:"Barbeque Nation",address:"varanasi",city:"varansi"},
       {name:"The Table",address:"varnasi",city:"varansi"},
       {name:"i love my wife",address:"varansi",city:"varansi"},
       {name:"Batti chokha",address:"varansi",city:"varansi"}
      ]


constructor(){
  restaurantList.foreach(printAllRestaurantByName = () =>{
   for(let i=0;i>this.restaurantList.length;i++){
   console.log(this.restaurantList[i].name)}})}}
Posted
Updated 7-Sep-22 7:12am

1 solution

The printAllRestaurantByName method is a member of the restaurantManager class, so you need an object of that class in order to call it. See JavaScript Classes[^].
 
Share this answer
 
Comments
Ujala 2022 9-Sep-22 2:15am    
Can u please help me with the code ??
Richard MacCutchan 9-Sep-22 3:14am    
Not really, as it is far from clear what you are trying to do. You need to look at your design and decide what the Javascript code is supposed to do, and how it can be actioned from the HTML.
Ujala 2022 9-Sep-22 4:13am    
SOLVED THE ERROR.Your suggestion helped a lot.Thank you so much!!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900