Home > Software engineering >  How to access returned HTML element
How to access returned HTML element

Time:08-28

This function defines an HTML element. document.getElementById returns the complete function, not only the h1 element as expected. How I only write what's inside of the two ` (return value).

function tokens() {
        return `
        <h1>test</h1>
    `;
}

document.getElementById('tokens').innerHTML = tokens;

CodePudding user response:

Change

document.getElementById('tokens').innerHTML = tokens;

to

document.getElementById('tokens').innerHTML = tokens();

CodePudding user response:

You have to call the function with parentheses () in order for it to execute and return the proper output:

document.getElementById('tokens').innerHTML = tokens();

CodePudding user response:

You've written tokens as if it is a variable. Since you want to run a function, add () to tokens. That would be tokens().

  • Related