views:

202

answers:

2

I'm doing some attribute parsing as a string, so I need to know how spaces are used in HTML/XHTML element attributes:

  <div id='myid' width='150px' />

Is this also correct?

  <div id = 'myid' width = '150px' />

If anyone knows other ways of iterating through attributes and their values using JavaScript, I'd be interested to know.

+6  A: 

Yes, both are correct. Rather than string parsing, you'll want to use the DOM. Check out jquery.

Al W
+2  A: 

The spaces are allowed. If you are parsing attributes why don't you let the browser parse? If you are using innerHTML than you get elements that have an attribute list

Norbert Hartl
innerHTML? Could you please elaborate?
Jenko
You can parse HTML by inserting the HTML into some element's innerHtml property. The browser parses the HTML and inserts it into the tree as the element's child nodes.
thomasrutter
Letting the browser parse the HTML sounds dangerous - wouldn't any <script> tags get executed?
Neall
Nope, the script tags are IMHO embed in the DOM but not evaluated
Norbert Hartl