user1590534
user1590534

Reputation: 322

JavaScript create clickable href

I have a simple stupid question. I want to add a URL which is clickable in JavaScript to my HTML

var a = document.createElement('a');
a.setAttribute('href', 'http://example.at');
$("#upcomingEvents").append('Please check our website. ' + a);

The URL appears, but it is not clickable, how can I change that?

Thanks!

Upvotes: 0

Views: 216

Answers (2)

sachleen
sachleen

Reputation: 31131

You have to put some text inside the link so there is something to click:

a.innerText='click me!';

And then you can't concatenate a string to a DOM element.

$("#upcomingEvents").append('Please check our website.');
$("#upcomingEvents").append(a);

Demo

Upvotes: 2

Halcyon
Halcyon

Reputation: 57719

Try it like this:

$("#upcomingEvents").append('Please check our website. ');
$("#upcomingEvents").append(a);

The + operator causes the DOMNode to be cast to a string, you don't want that.

Upvotes: 2

Related Questions