0

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!

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

Comments

2

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.

1 Comment

Might as well use .append().append()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.