Manipulating HTML with JavaScript: <label> element "for" attribute
November 18, 2009
/ Filed under: JavaScript, HTML
When manipulating HTML documents via JavaScript and the DOM, it's important to watch out for reserved-keyword-crossover issues, where certain HTML element or attribute names are actually reserved keywords in JavaScript. This is important to know when trying to declare HTML element attributes using JavaScript For example, let's say I want to create this HTML element using JavaScript:
I can't do something like this:
var div1 = document.createElement("div");
div1.class = "one";
The word "class" is reserved in JavaScript, and although you're intention is to declare the HTML elements' "class" attribute, this won't work. Instead, you need to do this: div1.className = "one"; "className" is a subtle alternative that will apply the "class" attribute to any HTML element. Side-note: I don't quite understand why it's called "className," when "classAttributeValue," or "classValue" would make more sense. HTML
|
Editor Picks
Email NewsletterSubscribe to the digest newsletter to receive posts by email: Recent Comments
Advertisements
|