6

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.

2
  • You might want to specify what language you're trying to do the parsing in - if in C, C#, JavaScript. If JavaScript, are you perhaps trying to your own HTML document from within a browser, or offline? Commented May 6, 2009 at 6:27
  • Note that the width attribute is (a) deprecated and (b) takes either an integer or percentage - don't specify px in HTML (but always specify units in CSS). The validator won't pick this up, because the DTD doesn't express this rule (and I don't think DTDs are capable of doing that either) Commented May 6, 2009 at 8:35

2 Answers 2

8

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

Sign up to request clarification or add additional context in comments.

Comments

2

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

5 Comments

innerHTML? Could you please elaborate?
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.
Letting the browser parse the HTML sounds dangerous - wouldn't any &lt;script&gt; tags get executed?
Nope, the script tags are IMHO embed in the DOM but not evaluated
script elements do not run in innerHTML. However, it is unsafe: <img src="//" onerror="alert()">. To make it safe, use document.implementation.createHTMLDocument

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.