0

js in my web app, i was just creating table with work, but sadly onClick on a Uncaught ReferenceError: (function) is not defined at HTMLTableRowElement.onclick

here is table code $("#usertable").append("<tr onclick=\"displaySmth('" + smth_value + "')\"><td>" + username + "</tr></td>");

and function to display smth_value

   displaySmth(somevalue){
    $("#smth_id").text(somevalue)
}
2
  • I think you'll need to share a bit more code. Where do you add the onclick listener? Commented Apr 19, 2018 at 16:06
  • oh sorry, i just failed while writing this question, updated it already, now you can see the onclick listener Commented Apr 19, 2018 at 16:36

1 Answer 1

1

With the way you're adding your click listener, the displaySmth function has to be a direct child of the window object; if you're using React.js then this probably isn't the case.

A more common way to add click listeners in React is to inject the function itself (rather than the name), like this:

$("#usertable").append(
    <tr onclick={displaySmth}><td>{username}</td></tr>
);

This is a touch more complicated when you want to use parameters in your listener, but the concept is the same.

Sign up to request clarification or add additional context in comments.

2 Comments

Are you sure that there is no need to user quotation marks? when im using simple $("#usertable").append( <tr><td>+username+</td></tr>) its not even giving me usernames in table
It depends a bit on where the code is; if it's in a file that get's compiled (which is most common with react) then quotation marks around html-ish objects aren't needed, but if it's in a non-compiled js file then they will be. This is a pretty good tutorial for getting started with tables in React

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.