This forum is now read-only. Please use our new forums at discuss.codecademy.com.
closing input tags with /> or just >???
Should input tags look like
<input type="text" name="example" />
<input type="text" name="example">
The exercise (Getting Input Section 2 Pt 2) says they should close with a /> but each one I see closes without the slash. I know from my geocities days that tags close with a slash but this course has taught me that a lot of standards have changed since then! So which one is the correct way (in real life, not just to pass codecademy), and why is the lesson giving conflicting information?
Realistically, the validity would depend on which doctype you declare.
In this context,
/> is valid with HTML 5 and XHTML doctypes and invalid with HTML 4.01 doctypes.
This stackoverflow question has more detailed information about exactly what's going on in your browser as well as links to the official W3C documentation.
I am also confused. In 2.2, the instruction says:
There is an input field with a type of "text". Notice that the input field doesn't have a closing tag. Instead it ends with a space and a forward slash.
but in the sample code, there is no such space and forward slash. The instruction is not consistent with the sample code.
It is a minor error on the course designer's part to say there is an /> and then forget to include it.
In older versions of HTML, there are tags that don't need a matching end tag. **** is one and **** is another.
As time went by, many in the HTML community felt that every HTML tag should have a corresponding ending tag. If no ending tag exists, then instead of just ending with an >, end with />
That is why you see some people using "**" just like that,
and others coding it **"" Some code <input > and others would code that <input />
Bottom line is both ways work. I'm going to go with the />'s myself because I like there to be an "ending" tag.
From the reference link provided by Will (first reply post):
"...a self-closing div will not validate. This is because a div is a normal element, not a void element. According to the spec, tags that cannot have any contents (known as void elements) can be self-closing*. This includes the following tags:
area, base, br, col, command, embed, hr, img, input,
keygen, link, meta, param, source, track, wbr
The "/" is completely optional on the above tags, however, so is not different from , but is invalid."