What is the difference between ID and Name

A common enough question when writing HTML is what is the difference between ID & Name and how are they used.

For example look at the following HTML:

<form action="/request" method="post">
    <label for="Status">Status</label>
    <select id="Status" name="Status">
        <option>Submitted</option>
        <option>Approved</option>
        <option>Rejected</option>
    </select>
    <button type="submit">Save</button>
</form>

Why have ID & Name set to the same value on the Select?

Without name set then the form post to “/request” will not contain the selected value of Status, equally with out an ID I will not be able to style the Select with CSS or work with it using Javascript.

Simply put:

ID is used for CSS and JavaScript. It has to be unique to the page.

Name is used for form handling by the server when a form is submitted via HTML. It has to be unique in a form.

Source: Difference between id and name attributes in HTML

Advertisements

Published by

Alan Feekery

Developer, Gamer, Musician, Cyclist and big Motorsport fan... enjoys the odd cup of coffee :)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s