Tweet Comment: Did you know, jQuery is able to…?

@prantor19 just twittered a list of useful jQuery Snippets for 2014:

Well, I’m not to sure about some of these snippets. Sometimes, I think, you should CSS like for example:
$('li:odd').css('background-color: #000');

I just don’t get it, why you cant just use CSS
li:nth-child(2n){background-color:#000;}

Well, there might be some reasons, why you have to use Javascript instead, but a Rocking Script as it says in the Title “Rocking and Useful jQuery Code Snippets For 2014”?

There are some others too, which would fit more into a title “Did you know, jQuery is able to…?“.

$('input [type="submit"]').attr('disabled', true);
Yes! With jQuery you can alter attributes!

Well really, what is so rocking about
var anchor = $('#anchor');
setTimeout(function() {
anchor.removeClass('current');
}, 3000);

???

So, despite the strange title, which really hasn’t proven for me, I do like this one:

$('img').error(function() {
$(this).attr('src', 'img/not_found.png');
});


$('img').load(function() {
console.log('images successfully loaded');
});

Its quite obvious, when you see it, but I haven’t thought about this possibility yet. Why should the load-function just work with window?? I will try this one for sure!

And this one here
$("p:not(:last-of-type)").after("
");

is quite nice to investigate further. As the jQuery Docs explain “Insert content, specified by the parameter, after each element in the set of matched elements”. So this piece of code puts a break between all &lt,p>-Tags, but not behing the last one, because :not(:last-of-type) I like this usage of the selectors and once again it shows the power of them.

Thanks for the Tweet!

The following two tabs change content below.
Seine erste Webseite hat David Remer 1998 in HTML verfasst. Wenig später war er fasziniert von DHTML und JavaScript. Heute konzentriert sich vor allem auf das Entwickeln von WordPress Themes und Plugins für Inpsyde. Außerdem hat er das Buch "WordPress für Entwickler" verfasst.

Latest posts by David Remer (see all)