How to get the value of an attribute in Javascript

后端 未结 3 749
灰色年华
灰色年华 2020-12-09 19:26

I have this input line which I am trying to extract the text of the value attribute:

    

        
3条回答
  •  盖世英雄少女心
    2020-12-09 19:54

    element.getAttribute("value") returns value which was set in the markup, which is not necessarily same as element.value.

    Also, value attribute of an element is only synchronized one way - from markup to the object and vice versa doesn't happen.

    So, if you want to get the value that is set programmatically, you need to write

    element.value
    

    else, if you need to get the value which was defined in the markup as

    
    

    you need to do element.getAttribute("value")

提交回复
热议问题