简体   繁体   中英

document.getElementById().value var + nod working

hello everyone so i want to make this part easier for the problem but i can't

need to change the following this code works

setTimeout(

    function() {
document.getElementById("coordinateTargetFirstTime").value = "427|501";}, (Math.random() * 1000) + 3000);

and change to

var template = "427|501";

setTimeout(

    function() {
document.getElementById("coordinateTargetFirstTime").value = " + template";}, (Math.random() * 1000) + 3000);

this code nod working help me thanx (+ template) is show + teplated ( nod 427|501 )

You are putting the variable sourrounded by double quoutes which makes it a string.

try this: document.getElementById("coordinateTargetFirstTime").value = template;

By the way where working with the dom I recomend using jQuery since it cross browser compatible and in this case it has a method called .val() or you can also use setAttribute('value')

Hope it works, cheers

As Iban has said you are making the template var a string. To enhance your readability maybe break it out as described below:

var template = ""
function run(template) {
    let id = document.getElementById("elementid")
    let result = id.value + template
    return result
}

let timeout = (Math.random() * 1000) + 3000)

setTimeout(run(template), timeout);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM