如何将var传递给getElementById?

时间:2016-03-06 18:36:49

标签: javascript html

function editar(clicked_id){
 var currentID = clicked_id;
alert(clicked_id);
}

我想将var currentID传递给getElementById

var tituloAgain = document.getElementById(clicked_id).value;
console.log(tituloAgain);

1 个答案:

答案 0 :(得分:0)

您可以将currentID的定义移到函数外部,使其成为全局变量。然后你可以在函数内部给它一个值,如下所示:

var currentID;
function editar(clicked_id){
    currentID = clicked_id;
    alert(clicked_id);
}
var tituloAgain = document.getElementById(currentID).value;
console.log(tituloAgain);