my jQuery tips

A collection of jQuery examples and tips

adding background image to an element

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

get clicked element

<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>

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.


$('.item.active').attr('class', 'item');

to be continued…

my jQuery tips

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s