1

I am generating a HTML element with an attribute via jQuery and then inserting it into the DOM. I am passing a class attribute into the element. See below:

jQuery('<li></li>', {  
    class: "myClass" 
});

I am receiving the following error in ie7 and ie8, on the line with class: "myCLass".

SCRIPT1028: Expected identifier, string or number

It works in ie9 and Firefox. Haven't tested any other browsers.

Is this method of generating an element with attributes not supported in ie7 and ie8? Or am I making a mistake in my script?

2 Answers 2

4

It is reserved. Use className or wrap it with quotes.

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

2 Comments

To be more specific, you can't use class as the key. You can, however, use "class" or rename it to className.
wrapping in quotes fixed the issue. Thank you.
0

Either use className, or use prop.

// jQuery way
$('<div></div>').prop('class', 'myClass')

// Vanilla js way
$('<div></div>')[0].className = 'myClass'

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.