-1

I am trying to get a webelement's id and class if it has one. The first check using the id works fine, but the one checking the class doesn't. I don't know why but ' id ' returns true but ' class ' does not return anything. Any ideas on why this doesn't work and how to make it work?

 var id = currentElement.id;
      if(id)
      {
        targetDocument.write(" id=\""+currentElement.id+"\"");
      }
   var class = currentElement.className;
      if(class)
      {
        targetDocument.write(" class=\""+currentElement.className+"\"");
      }
kusold
  • 383
  • 1
  • 7
  • 16
  • 6
    `class` is a reserved word... http://stackoverflow.com/questions/7524618/why-is-class-a-reserved-word-in-javascript –  Aug 08 '13 at 19:20
  • 2
    Open your browser's developer console. This should be the first thing you do when troubleshooting code. –  Aug 08 '13 at 19:24
  • sorry. I'm super new to this. Thanks remyabel now I know. dystroy, this is just a snippet and it does compile – kusold Aug 08 '13 at 19:34

1 Answers1

0

In JavaScript, class is a reserved word. try className (for example) instead.

haim770
  • 48,394
  • 7
  • 105
  • 133