SoftTimur SoftTimur - 5 months ago 18
HTML Question

Adding an onclick event to a text without breaking the line

I need to add an

onclick
event (rather than a hyperlink) to a text. But using
p
tag breaks the line:

enter image description here

Here is the code and JSBin. Could anyone help?

<!DOCTYPE html>
<html>
<body>
See our <p onclick="openWindow()" style="cursor: pointer; color:blue">support page</p> and other things...
<script>
function openWindow() {
window.open("https://www.google.com");
}
</script>
</body>
</html>

Answer

Actually you should use <a>.

You can still use onclick. Just give it href="#"

See our <a onclick="openWindow();" href="#">support page</a> and other things...
  1. Semantically correct

  2. Saves the styling overhead of the <span>

  3. If you globally style <a> elements in your page, it will get the style too.

Side Note

If your only intention is to open the link in a new tab (as in your example) then use target instead of JS

See our <a target="_blank" href="https://www.google.com">support page</a> and other things...