0

I am looping through an array, in my loop i have this function that sets the array item id to localstorage

 <% if (result.length > 0) { %>
      <%result.map((item,index) => { %>
         <%  function check() { 
          localStorage.setItem("product_id", item.id); 
         }%>
         <a onclick='check()' href="#">
          <span>Edit</span>
        </a>

     <% }) %>
 <% }


This is where an calling the function, still inside the loop

    <a onclick='check()' href="#">
       <span>Edit</span>
    </a>

I want onclick of this anchor tag to call that function at the top, but its giving me this error


    products:329 Uncaught ReferenceError: check is not defined
        at HTMLAnchorElement.onclick (products:329)
    onclick @ products:329

CodaBae
  • 275
  • 2
  • 6
  • 16
  • Your syntax isn't valid or the snippet is incomplete. Anyway, you have defined check inside of the map call. – Aluan Haddad Feb 28 '20 at 21:14
  • @AluanHaddad yes its incomplete, i will quickly update it, and yes the function is in a map and its also been called in the map function, is that wrong? – CodaBae Feb 28 '20 at 21:23
  • @AluanHaddad it has be updated – CodaBae Feb 28 '20 at 21:28
  • A JavaScript function defined in the EJS program (which runs on the server) is not accessible in a completely different JavaScript program running in the rendered webpage in the browser. – Quentin Feb 28 '20 at 21:56

0 Answers0