Difference between id and name attributes in HTML

前端 未结 19 3157
感动是毒
感动是毒 2020-11-22 05:17

What is the difference between the id and name attributes? They both seem to serve the same purpose of providing an identifier.

I would lik

19条回答
  •  孤城傲影
    2020-11-22 05:52

    There is no literal difference between an id and name.

    name is identifier and is used in http request sent by browser to server as a variable name associated with data contained in the value attribute of element.

    The id on the other hand is a unique identifier for browser, client side and JavaScript.Hence form needs an id while its elements need a name.

    id is more specifically used in adding attributes to unique elements.In DOM methods,Id is used in Javascript for referencing the specific element you want your action to take place on.

    For example:

    
    
    
    

    Same can be achieved by name attribute, but it's preferred to use id in form and name for small form elements like the input tag or select tag.

提交回复
热议问题