-
Notifications
You must be signed in to change notification settings - Fork 0
/
divpush.js
63 lines (55 loc) · 1.75 KB
/
divpush.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
$(function() {
$('#sSort').hide();
$('#cSort').hide();
});
var oneDiv = '<div></div>';
var newDiv = function(){
$('p').after(oneDiv);
};
var aClass = function(){
$('div:first-of-type').addClass(rColor[randC]).addClass(rSize[randS]).text(randC);
};
$('#push:button').on('click', function(){
for (var i=0; i < 501; i++) {
dPush(i);
};
$('#sSort').show();
$('#cSort').show();
$('#push').hide();
$('p').hide();
});
//Scott Schmidt helped me get my variables in the proper scope
var dPush = function(){
randC = Math.floor(Math.random()*10);
randS = Math.floor(Math.random()*10);
newDiv();
aClass();
};
var rColor =["red", "orange", "yellow", "green", "blue",
"indigo", "violet", "white", "gray", "black"];
var rSize = ["size0", "size1", "size2", "size3", "size4",
"size5", "size6", "size7", "size8", "size9",];
//I found this sort via a google search and modified it.
//It was originally set up to sort li tags to keep his lists ordered.
//http://www.onemoretake.com/2009/02/25/sorting-elements-with-jquery/
$('#sSort:button').on('click', function(){
var mylist = $('section');
var listitems = mylist.children('div').get();
listitems.sort(function(a, b) {
//Craig Chaille suggested sorting by width.
var compA = $(a).width();
var compB = $(b).width();
return (compA < compB) ? -1 : (compA > compB) ? 1 : 0;
})
$.each(listitems, function(idx, itm) { mylist.append(itm); });
});
$('#cSort:button').on('click', function(){
var mylist = $('section');
var listitems = mylist.children('div').get();
listitems.sort(function(a, b) {
var compA = $(a).text();
var compB = $(b).text();
return (compA < compB) ? -1 : (compA > compB) ? 1 : 0;
})
$.each(listitems, function(idx, itm) { mylist.append(itm); });
});