Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the preferred way to create a jQuery object with attributes?

When creating jQuery objects I've used the following syntax lately, as described here:

var $el = $('<div/>', {
   class: 'class-1 class-2'
});

Safari 5.0.5 gives a syntax error at the point where I use the above construction.

Removing the second argument and adding classes with addClass removes the error, but seems rather unelegant.

How do you create your objects? I tried with attr({class: 'class-1'}), as well, but received the same syntax error.

like image 946
vorpyg Avatar asked May 17 '11 12:05

vorpyg


1 Answers

You can't use class; it's a reserved word.

Use className instead:

var $el = $('<div/>', {
   className: 'class-1 class-2'
});
like image 65
lonesomeday Avatar answered Oct 06 '22 00:10

lonesomeday