Adding Selector Support to jQuery.get() to get page fragments
Just wrote a little plugin whilst working on a project because I keep finding myself using this functionality and having to write a handler in my application code. This plugin can be dropped in, just like jQuery Animate Enhanced and weighs in at less than 1KB, and it will enable you to quickly grab a partial or fragment of the AJAX endpoint. Here's some dummy code:
var sillyWords = ["incredible", "awesome", "amazing", "fantastic"]; | |
$('button#start').click(function() { | |
var button = $(this).attr('disabled', 'disabled'); | |
$.get('endpoint/data.html #target', function(data) { | |
// do something amazing with the data | |
if (data) { | |
// fetch word | |
var word = sillyWords[Math.floor(Math.random() * sillyWords.length)]; | |
// replace | |
var output = data.replace(/cool/ig, word); | |
// write to DOM | |
$('.target').html(output); | |
} | |
button.removeAttr('disabled'); | |
}); | |
}); |
There's not really much more I can say about this. Its unobtrusive, light weight and will simply hand over to the native jQuery.get() method if no selector is supplied.