Tag Archives: jquery

The Difference Between .live() and .delegate and Which One Should be Used

There are so many posts about that topic out there, but I’d like to be rather breve! The delegate() method comes with the last major version of the jQuery – 1.4 and it gives a context to the event. So please use delegate() instead of live(), just because not everything’s attached to the document element, but to the selected object.

Secure localStorage? Now that’s a good question!

Nicholas Zakas posted today a very very interesting post about one of the most interesting and useful features in HTML5 – localStorage. What’s really impressing is that everybody’s using it, including me, with no fear of security. But security is never strong enough, so it’s quite interesting to listen up the guru!

I recently posted a localStorage wrapper for jQuery in form of a simple plugin. What I miss there is the security.

Definitely I’ve to change it a bit!

jQuery Get the Id of the Current Element

Useful Tips

Sometimes is really useful not only to read the docs, but to be aware of what the community is writing. Although you get familiar with a library or framework, it happens sometimes to discover very very useful things in them, kind of snippets, that you’ve missed all the time.

In that sense, I’m going to talk about something really small as code in jQuery, but that is quite used. The id selector.

It just so happens that after you discover the selection of an attribute with .attr() method you start selecting even the ids with it.

$('element').attr('id');

Which of course works quite well, but there is a built-in method that has clearer syntax and … better performance – the id.

Think of something like that:

$('element').click(function() {
    alert($(this).attr('id'));
}

It can be replaced with:

$('element').click(function() {
    alert($(this).id);
}

It’s cool!

jQuery cssText Helps You Improve Browser Reflows

cssText

You know you can manage to redraw an element with single browser reflow. Instead of using .style.property … you can simply add all CSS properties you’d like to change with simply appending to style.cssText property.

var csstxt = $('#selector').css('cssText') + ';top:100;left:100;border:1px solid red;color:#f00;';
$('#selector').css('cssText', csstxt);

That code is a replacement for

$('#selector').css({
   left : '100px',
   top : '100px',
   border : '1px solid red',
   color : '#f00'
});

enjoy!