Sometimes, we want to pass a string parameter in an onclick function with JavaScript.
In this article, we’ll look at how to pass a string parameter in an onclick function with JavaScript.
How to pass a string parameter in an onclick function with JavaScript?
To pass a string parameter in an onclick function with JavaScript, we call a function that takes the parameters we want inside the click handler.
For instance, we write
const inputElement = document.createElement("input");
inputElement.type = "button";
inputElement.addEventListener("click", () => {
gotoNode(result.name);
});
to call addEventListener
to add a click listener.
In it, we call gotoNode
with result.name
.
Conclusion
To pass a string parameter in an onclick function with JavaScript, we call a function that takes the parameters we want inside the click handler.