Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Question on quotes.

Status
Not open for further replies.

theniteowl

Programmer
May 24, 2005
1,975
US
Hi all,
I feel like this may be a silly question but I have never seen any information about it.

When working in HTML, when is it necessary to use quotes in your code?
I have always used quotes around values for parameters but I see SOOO many code examples where they have been left off.
I also find that when I use .innerHTML to grab code from inside a div tag that it is returned without the quotes that were originally in the HTML.

Are quotes only necessary when the value contains spaces?
Is using quotes all the time the correct method?
Is not using quotes supported among all the different browsers?

Anyone have a rundown on this? It's mostly curiosity but it can sure simplify coding when building complex HTML strings through code and having to dance around how to embed the quotes into the string.

TIA.
Nite

Paranoid? ME?? WHO WANTS TO KNOW????
 
Double quotes around your html attribute values is required for xhtml validation. It is considered good practice anyway and despite both those it is necessary for values with spaces in as you mentioned.

Although we can get away with it now, in order to help towards future-proofing our websites I would recommend that website writers get used to doing it now.

Haven't really used .innerHTML much so I'm afraid I can't relly comment on that.



Tony
 
theniteowl, try this:

Code:
<input type=text name=t1 value=hello there />

*cLFlaVA
----------------------------
[tt]I already made like infinity of those at scout camp...[/tt]
beware of active imagination: [URL unfurl="true"]http://www.coryarthus.com/[/url]

BillyRayPreachersSonIsTheLeetestHax0rDude
[banghead]
 
Just to comment on Tony's answer, XHTML requires quotes around all attribute values, but not necessarily double quotes. Single quotes are ok as well.

As you can see from lazy cLFlaVA's (har har) example when attributes are not quoted, browser has no way of knowing where the attribute ends -- if it contains multiple words and will cut it at first space.
 
To answer the question, HTML has different rules to XHTML. In HTML, purely numeric values do not need to be quoted; any value containing letters, spaces or punctuation needs quotes (single or double). So this is valid HTML (but invalid XHTML):
Code:
<img src='kitty.jpg' height=200 width=300 alt="My cat, fluffy">
That said, like FesterSXS, think it's good practice to quote all attributes regardless.

-- Chris Hunt
Webmaster & Tragedian
Extra Connections Ltd
 
I have always used double quotes for all attributes anyway, I have just seen code without so many times that I have wondered about it.

Paranoid? ME?? WHO WANTS TO KNOW????
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top