How to get the value of an attribute in Javascript

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

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

    

        
3条回答
  •  Happy的楠姐
    2020-12-09 20:06

    Node values and Element Attributes are different parts of an html tag. So, you have to use element.value instead.

    This is a an example, to show you how you can fetch value, data, attribute from an input field.

    The HTML input field.

    
    

    and the javascript.

    var el = document.getElementById("user-profile"); 
    
    console.log(el.value) // Simon
    console.log(el.getAttribute("id")) // profile
    console.log(el.dataset.nationality) // Eritrean
    

提交回复
热议问题