Categories
JavaScript Answers

How to pass a string parameter in an onclick function with JavaScript?

Spread the love

To pass a string parameter in an onclick function with JavaScript, we add a click listener that calls the function that takes the string.

For instance, we write

const inputElement = document.createElement("input");
inputElement.type = "button";
inputElement.addEventListener("click", () => {
  gotoNode(result.name);
});

document.body.appendChild(inputElement);

to create an input with createElement.

We set the type property to set the type attribute.

Then we call addEventListener to add a click listener to the input that calls the gotoNode function with the string.

Then we call appendChild to append the input as the last child of the body element.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *