The correct syntax to return a variable to HTML pages

The correct syntax to return a variable to HTML pages
0

#1

Hi,

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


#2

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


#3
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.setAttribute('href',urlFullTmdb);
    el.setAttribute('target','_blank');
    el.innerHTML = title; 
    var  body = document.querySelector('body');
    body.appendChild(el);

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

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

#4

Thanks @alphaoliveira and @codejunky for your answer.