Categories
JavaScript Answers

How to make HTML text clickable without making it a hyperlink with JavaScript?

Sometimes, we want to make HTML text clickable without making it a hyperlink with JavaScript.

In this article, we’ll look at how to make HTML text clickable without making it a hyperlink with JavaScript.

How to make HTML text clickable without making it a hyperlink with JavaScript?

To make HTML text clickable without making it a hyperlink with JavaScript, we can select the element with the text.

Then we set its onclick property to a function that runs when we click on it.

For instance, we write:

<p>
  hello
</p>

to add a p element.

Then we write:

const p = document.querySelector('p')
p.onclick = () => {
  console.log('clicked')
}

to select the p element with document.querySelector.

Then we set its onclick property to a function that runs when we click on the p element.

As a result, we see the 'clicked' is logged when we click on ‘hello’.

Conclusion

To make HTML text clickable without making it a hyperlink with JavaScript, we can select the element with the text.

Then we set its onclick property to a function that runs when we click on it.

Categories
JavaScript Answers

How to add a click event handler to an image with JavaScript?

Sometimes, we want to add a click event handler to an image with JavaScript.

In this article, we’ll look at how to add a click event handler to an image with JavaScript.

How to add a click event handler to an image with JavaScript?

To add a click event handler to an image with JavaScript, we can select the image and set its onclick property to a click event handler function.

For instance, we write:

<img src="https://i.picsum.photos/id/572/200/300.jpg?hmac=Rt4zD8IxoA-nMVDrBQ72mgbTVRfQ6OwW3MhWy_3lpdk" />

to add an image.

Then we write:

const img = document.querySelector('img')
img.onclick = () => {
  console.log('clicked')
}

Then we select the img element with document.querySelector.

Next, we set its onclick property to a function that runs when we click on the image.

As a result, we see 'clicked' logged when we click on the image.

Conclusion

To add a click event handler to an image with JavaScript, we can select the image and set its onclick property to a click event handler function.

Categories
JavaScript Answers

How to append a div to the end of document with jQuery?

Sometimes, we want to append a div to the end of document with jQuery.

In this article, we’ll look at how to append a div to the end of document with jQuery.

How to append a div to the end of document with jQuery?

To append a div to the end of document with jQuery, we can call the append method on the body element with a string with the div’s code as the argument.

For instance, we write:

$(document.body).append('<div>hello world</div>');

to select the body element with $(document.body).

Then we call append with '<div>hello world</div>' add the div as the last child of the body element.

As a result, we should see ‘hello world’ on the screen.

Conclusion

To append a div to the end of document with jQuery, we can call the append method on the body element with a string with the div’s code as the argument.

Categories
JavaScript Answers

How to parse numbers with comma decimal separators in JavaScript?

Sometimes, we want to parse numbers with comma decimal separators in JavaScript.

In this article, we’ll look at how to parse numbers with comma decimal separators in JavaScript.

How to parse numbers with comma decimal separators in JavaScript?

To parse numbers with comma decimal separators in JavaScript, we can remove all the comma separators with the JavaScript string’s replace method.

Then we can use Number to parse the number.

For instance, we write:

const n = '123,456.789'
const num = n.replace(/,/g, '');
const parsed = Number(num)
console.log(parsed)

to call n.replace to replace all the commas with empty strings.

Then we call Number with num to convert num to a number.

As a result, parsed is 123456.789.

Conclusion

To parse numbers with comma decimal separators in JavaScript, we can remove all the comma separators with the JavaScript string’s replace method.

Then we can use Number to parse the number.

Categories
JavaScript Answers

How to refresh the page on click of a button with JavaScript?

Sometimes, we want to refresh the page on click of a button with JavaScript.

In this article, we’ll look at how to refresh the page on click of a button with JavaScript.

How to refresh the page on click of a button with JavaScript?

To refresh the page on click of a button with JavaScript, we can call the window.location.reload method in the button click event handler.

For instance, we write:

<button type="button">Close</button>

to add a button.

Then we write:

const button = document.querySelector('button')
button.onclick = () => {
  window.location.reload();
}

to select a button with document.querySelector('button').

Then we set the button.onclick property to the click handler function.

In it, we call window.location.reload to reload the page.

Conclusion

To refresh the page on click of a button with JavaScript, we can call the window.location.reload method in the button click event handler.