I am creating div the following way
$('<div/>', {
"class" : 'draggable player' + player + ' unit unit' + unit
})
And I can’t find what other properties I can assign to it the same way I assign class. especially if I can assign data or attr?
According to the
jQuery()function documentation (alias of$()), you can define attributes, events, and methods within that second parameter.So yes, anything you’d define using
attr()is fair game. That includes “data-whatever” attributes (which are oddly accessible via$elem.data('whatever')), however they will not be saved tojQuery.datalike any variables defined via$elem.data('name', 'value')(at least until you call$elem.data('whatever')– see http://api.jquery.com/jQuery.data/#entry-longdesc-1).So to clarify:
will create this element, and return a jQuery object containing it:
From there, you will be able to do:
Of course,
$elem.attr('data-test')will also work.