Categories
JavaScript Answers

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

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.

Categories
JavaScript Answers

How to scroll to an element using JavaScript?

To scroll to an element using JavaScript, we call the scrollIntoView method.

For instance, we write

document.getElementById("divFirst").scrollIntoView();

to select the element with getElementById.

Then we call scrollIntoView to scroll it into view.

Categories
JavaScript Answers

How to add onload event to a div element with JavaScript?

To add onload event to a div element with JavaScript, we use the MutationObserver constructor.

For instance, we write

const observer = new MutationObserver((mutationList, obsrvr) => {
  const divToCheck = document.querySelector("#foo");
  if (divToCheck) {
    console.log("div is loaded now");
    obsrvr.disconnect();
    return;
  }
});

const parentElement = document.querySelector("parent-static-div");
observer.observe(parentElement, {
  childList: true,
  subtree: true,
});

to create the observer object by calling MutationObserver with a callback that checks if the element with ID foo with querySelector.

Then we check if it’s not null with the if statetment.

If it’s not null, then we call disconnect to disconnect the observer.

Next, we select its parent element with querySelector.

And then we call observer.observe with the parentElement and an object that has childList and subtree set to true to watch for changes of the elements down the hierarchy.

Categories
JavaScript Answers

How to call multiple JavaScript functions in onclick event?

To call multiple JavaScript functions in onclick event, we use addEventListener.

For instance, we write

const el = document.getElementById("id");

el.addEventListener("click", () => {
  alert("hello world");
});
el.addEventListener("click", () => {
  alert("another event");
});

to select the element with getElementById.

Then we call addEventListener to add a click listener to the element.

We call it twice with difference functions to add all the functions as click listeners.

So they’ll all be called when we click on the element.

Categories
JavaScript Answers

How to get the entire document HTML as a string with JavaScript?

To get the entire document HTML as a string with JavaScript, we use the innerHTML property.

For instance, we write

const markup = document.documentElement.innerHTML;
console.log(markup);

to get the htmlk element with document.documentElement.

Then we get the innerHTML property to the markup of its content as a string.