What's the difference between document.getElementById(“test”).value and document.getElementById(“test”)[removed]

后端 未结 5 1887
心在旅途
心在旅途 2021-01-01 00:37
document.getElementById(\"test\").value

document.getElementById(\"test\").innerHTML

Does the first mean the address and the second mean the value

5条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-01 01:34

    It has to do with how some tags work based on their attributes where others work on the text between the opening and closing tags.

    .value retrieves whatever value is set for the value attribute of the tag. .innerHTML retrieves whatever is in between the opening and closing tag.

    For example if the HTML tag was

    and you used the JavaScript
    var name = document.getElementById('user_name').value
    would declare a variable name and give it the value "Enter name here" (assuming the user didn't change it). On the other hand if you have HTML like

    blah blah

    then you would use
    var text = document.getElementById('abc')
    and that would set the variable text to "blah blah".

提交回复
热议问题