Search code examples
javascriptwordpressurlappendhref

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!


Solution

  • 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