The correct syntax to return a variable to HTML pages


I have a variable called title

var title;

What is the correct way to write the code below, so variable title will show up its value in HTML pages ?

document.getElementById("title").innerHTML = "title";

Thanks for your help

Well you need to change it to the following:
document.getElementById("title").innerHTML = "<a href = '" + urlFullTmdb + "' " + "target='_blank'>" + title + "</a>";

var title = "something";
document.getElementById("title").innerHTML = "<a href ='"+ urlFullTmdb + "' target='_blank'>" + title + "</a>";

I’m assuming that you want to put this element inside another that has an id = “title”. Maybe this is not what you intend to do.
Do you want to create an element?

    var urlFullTmdb ='someurl'    
    var title = 'something';
    var el = document.createElement("a");
    el.innerHTML = title; 
    var  body = document.querySelector('body');

If you already have the anchor element, give to it the id “title” and then

var title = 'something";
document.getElementById("title").innerHTML = title;

Thanks @alphaoliveira and @codejunky for your answer.

1 Like