0

I am getting an error for the following code.

function getDelAction(whatToDelete){
    return "<i class='fas fa-trash-alt text-danger' onclick=\"deleteRecord(this.closest('tr'), whatToDelete);\"></i>";
}

Created a function that returns a string representation of an element. When I run the code and click on the icon, JS complains about the whatToDelete argument I am passing to deleteRecord function which the function getDelAction is accepting as parameter. If I create the same variable outside the function and pass it like this it works. What is the problem?

Calling the function like getDelAction("customer");

Thanks!

Maseed
  • 533
  • 5
  • 19
  • 1
    Best to [avoid inline handlers](https://stackoverflow.com/a/59539045), they have way too many problems to be worth using nowadays, such as a demented scope chain and quote escaping issues. Attach event listeners properly using Javascript with `addEventListener` instead. – CertainPerformance Jan 23 '21 at 06:42

1 Answers1

0

try to return the string using template literals so that whatToDelete is passed as a variable:

    return `<i class='fas fa-trash-alt text-danger' onclick=\"deleteRecord(this.closest('tr'),${whatToDelete});\"></i>`;

And like other people mentioned, try to avoid this sort of stuff

misha1109
  • 358
  • 2
  • 5