Pass variable in document.getElementByid in javascript

前端 未结 4 1115
有刺的猬
有刺的猬 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条回答
  •  囚心锁ツ
    2020-12-06 23:59

    Why are you prefixing and post-fixing ' characters to the name string? ben_name is already a string because you've appended '_name' to the value.

    I'd recommend doing a console.log of ben_name just to be sure you're getting the value you expect.

    the way to use a variable for document.getElementById is the same as for any other function:

    document.getElementById(ben_name);
    

    I don't know why you think it would act any differently.

提交回复
热议问题