Pass variable in document.getElementByid in javascript

前端 未结 4 1157
有刺的猬
有刺的猬 2020-12-06 23:44

I have a variable account_number in which account number is stored. now i want to get the value of the element having id as account_number. How to do it in javascript ?

4条回答
  •  Happy的楠姐
    2020-12-06 23:56

    Are you storing just an integer as the element's id attribute? If so, browsers tend to behave in strange ways when looking for an element by an integer id. Try passing account_number.toString(), instead.

    If that doesn't work, prepend something like "account_" to the beginning of your elements' id attributes and then call document.getElementById('account_' + account_number).value.

提交回复
热议问题