How Do I Write This Shorter With JQuery?
Can someone help me. I want to write this code better and shorter. So how do I write this jQuery code snippet shorter and better? $('#empty_trash').click(function() { $('.ward
Solution 1:
Perhaps with a loop?
$("#empty_trash").click(function() {
var elem = $('.wardopeskab ul');
for(var i = 0; i < 10; i++) {
elem.prepend('<li class="underbukser"></li>');
}
for(var i = 0; i < 10; i++) {
elem.prepend('<li class="t-shirt"></li>');
}
});
Preferably you wouldn't use the magic numbers; 10 should have a name of some sort. I'm not sure what you're trying to do, so I'm not sure what it would be named.
Solution 2:
A little raw JavaScript power:
$('.wardopeskab ul')
.prepend(
new Array(11).join('<li class="underbukser">underbukser</li>') +
new Array(11).join('<li class="t-shirt">t-shirt</li>')
);
Fiddle: http://jsfiddle.net/8yEhE/5/
Note: The number 11
in new Array(11)
is 1 more than the times it will be written out.
Solution 3:
Use loops, for loops, while loops, etc:
$("#empty_trash").click(function () {
while ( /* Loop Condition Here */ ) {
$('.wardopeskab ul').prepend('<li class="underbukser"></li>');
}
while ( /* Loop Condition Here */ ) {
$('.wardopeskab ul').prepend('<li class="t-shirt"></li>');
}
});
Post a Comment for "How Do I Write This Shorter With JQuery?"