0
var ul=document.createElement("UL");
ul.Class="nav nav-pills nav-stacked";                   

var li=document.createElement("LI");
var x=document.createElement("a");
var linkText=document.createTextNode("text for link");

x.appendChild(linkText);
x.title="title of link";
x.href="link address";

li.appendChild(x);
ul.appendChild(lidom);
insertHere.appendChild(ul);

when I execute the above code the style in "nav nav-pills nav-stacked" is not applied

Kundan Singh Chouhan
  • 13,952
  • 4
  • 27
  • 32
Dilkush
  • 113
  • 1
  • 2
  • 15
  • *"how to add a class attribute in a ul tag"* Not to nitpick, but because it's an important distinction: That's not a "`ul` tag". It's a `ul` **element**. – T.J. Crowder Aug 09 '15 at 18:09

1 Answers1

2

Class should be className:

ul.className="nav nav-pills nav-stacked";

...because JavaScript is case-sensitive, so Class and class aren't the same thing, and because when the DOM bindings for JavaScript were being created, they couldn't use class because at that time, in JavaScript, you couldn't write ul.class = ... because class was a reserved word and you couldn't use it in a property literal like that. (You can now, as of ES5 in 2009, but of course that was more than a decade after the bindings were created.)

The other reflected attribute with an unusual name is htmlFor, which is for the for attribute (for instance, on label elements).

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875