my jQuery tips


A collection of jQuery examples and tips

adding background image to an element

[sourcecode language="javascript"]
<script type="text/javascript">
$(function () {
$('.hero-unit').css('background-image', 'url(/Content/images/sky.jpg)');
});
</script>
[/sourcecode]

get clicked element

[sourcecode language="javascript"]
<script type="text/javascript">
$(function () {
$('.thumbnail').click(function (e) {
e.preventDefault();

var foodName = $(this).attr('data-food-name');
var foodDescription = $(this).attr('data-food-description');

$('#modal_food_description').text(foodDescription);
$('#modal_food_image').attr('src', '../../Content/images/' + foodName + '_med.jpg').attr('alt', foodName);
$('#modal_food').modal();
});
})
</script>
[/sourcecode]

Selecting an element with multiple classes

When you select an element with a class you do $('.claasname'). Then how would you do with multiple classes? you add the additional class without space.

[sourcecode language="javascript"]

$('.item.active').attr('class', 'item');[/sourcecode]

to be continued...