Yes, you can do that:
$('.obj').get(0).key = 'value';
or
$('.obj').get(0)['key'] = 'value'
Though it's better to use .data():
$('.obj').data('key', 'value');
Tips
1. You can chain multiple calls, like this:
$('.obj').css('color', 'red').data('key', 'value');
2. You can pass an object to .css():
$('.obj').css({
'width': 100,
'height': 100
});
3. If you don't change other CSS properties, you can use .width()and .height() to set and get an element's width:
HTML
<div id='box'></div>
CSS
#box {
width: 200px;
height: 100px;
}
JavaScript
var $box = $('#box');
// 200x100
alert( $box.width() + 'x' + $box.height() );
4. You may have noticed in the previous example that I saved a reference to $('#box') in a variable. There are some cases when you can't use chaining, like this:
var $box = $('#box');
setTimeout(function() {
$box.animate({ 'width': 100 }, 100);
}, 1000);
setTimeout(function() {
$box.animate({ 'height': 200 }, 100);
}, 1000);
If you have to do that, always save a reference to the element – that's called caching. Otherwise jQuery would have to search for it multiple times.
Yes, you can do that:
$('.obj').get(0).key = 'value';
or
$('.obj').get(0)['key'] = 'value'
Though it's better to use .data():
$('.obj').data('key', 'value');
Yes, you can do that:
$('.obj').get(0).key = 'value';
or
$('.obj').get(0)['key'] = 'value'
Though it's better to use .data():
$('.obj').data('key', 'value');
Tips
1. You can chain multiple calls, like this:
$('.obj').css('color', 'red').data('key', 'value');
2. You can pass an object to .css():
$('.obj').css({
'width': 100,
'height': 100
});
3. If you don't change other CSS properties, you can use .width()and .height() to set and get an element's width:
HTML
<div id='box'></div>
CSS
#box {
width: 200px;
height: 100px;
}
JavaScript
var $box = $('#box');
// 200x100
alert( $box.width() + 'x' + $box.height() );
4. You may have noticed in the previous example that I saved a reference to $('#box') in a variable. There are some cases when you can't use chaining, like this:
var $box = $('#box');
setTimeout(function() {
$box.animate({ 'width': 100 }, 100);
}, 1000);
setTimeout(function() {
$box.animate({ 'height': 200 }, 100);
}, 1000);
If you have to do that, always save a reference to the element – that's called caching. Otherwise jQuery would have to search for it multiple times.