Categories
JavaScript Answers

How to change href of an a tag on button click with JavaScript?

Sometimes, we want to change href of an a tag on button click with JavaScript

In this article, we’ll look at how to change href of an a tag on button click with JavaScript.

How to change href of an a tag on button click with JavaScript?

To change href of an a tag on button click with JavaScript, we can set the href property of the anchor element.

For instance, we write

<a href="#" id="abc">foo</a> <a href="#" id="myLink">bar</a>

to add 2 anchor elements.

Then we write

document.getElementById("myLink").onclick = (e) => {
  e.preventDefault();
  document.getElementById("abc").href = "example.com";
};

to get the anchor element with ID myLink with

document.getElementById("myLink")

Then we set its onclick property to a function that changes the href attribute of the anchor element with ID abc on click with

document.getElementById("abc").href = "example.com";

We call preventDefault to stop the default behavior of the link, which is to navigate to the URL set as the value of the href attribute.

Then we can run the rest of the code in the function.

Conclusion

To change href of an a tag on button click with JavaScript, we can set the href property of the anchor element.

Categories
JavaScript Answers

How to call a function periodically in JavaScript?

Sometimes, we want to call a function periodically in JavaScript.

In this article, we’ll look at how to call a function periodically in JavaScript.

How to call a function periodically in JavaScript?

To call a function periodically in JavaScript, we call the setInterval function.

For instance, we write

const intervalId = setInterval(() => {
  console.log("Interval reached every 5s");
}, 5000);

to call setInterval with a callback that runs every 5 seconds.

We specify the period with the 2nd argument in milliseconds.

setInterval returns the ID number of the timer.

And we can use clearInterval with the timer ID number to stop the timer.

Conclusion

To call a function periodically in JavaScript, we call the setInterval function.

Categories
JavaScript Answers

How to create a link using JavaScript?

Sometimes, we want to create a link using JavaScript.

In this article, we’ll look at how to create a link using JavaScript.

How to create a link using JavaScript?

To create a link using JavaScript, we can use the createElement method.

For instance, we write

const a = document.createElement("a");
const linkText = document.createTextNode("my title text");
a.appendChild(linkText);
a.title = "my title text";
a.href = "http://example.com";
document.body.appendChild(a);

to call createElement with 'a' to create an anchor element.

Then we call createTextNode to create a text node.

Then we call a.appendChild with linkText to use linkText as the text content of the anchor element.

We set the title attribute by setting a.title.

The href attribute is set by setting a.href.

And finally, we call document.body.appendChild with a to append the anchor element as the last child of the body element.

Conclusion

To create a link using JavaScript, we can use the createElement method.

Categories
JavaScript Answers

How to make a form open a new window showing the result after submitting a POST request with JavaScript?

Sometimes, we want to make a form open a new window showing the result after submitting a POST request with JavaScript.

In this article, we’ll look at how to make a form open a new window showing the result after submitting a POST request with JavaScript.

How to make a form open a new window showing the result after submitting a POST request with JavaScript?

To make a form open a new window showing the result after submitting a POST request with JavaScript, we set the target attribute of the form to _blank.

To do this, we write

form.setAttribute("target", "_blank");

to call form.setAttribute with 'target' and '_blank' to make the form element open a new window after the form is submitted.

Conclusion

To make a form open a new window showing the result after submitting a POST request with JavaScript, we set the target attribute of the form to _blank.

Categories
JavaScript Answers

How to turn a string to a JSON object in Node.js?

Sometimes, we want to turn a string to a JSON object in Node.js.

In this article, we’ll look at how to turn a string to a JSON object in Node.js.

How to turn a string to a JSON object in Node.js?

To turn a string to a JSON object in Node.js, we can use the JSON.parse method.

For instance, we write

const obj = JSON.parse(yourJsonString);

to call JSON.parse with the yourJsonString JSON string to parse it into a JavaScript object.

Conclusion

To turn a string to a JSON object in Node.js, we can use the JSON.parse method.