Created
August 12, 2015 13:04
-
-
Save 1Marc/3d1b99938d679ba994c1 to your computer and use it in GitHub Desktop.
random DOM nodes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var comments = document.querySelectorAll('#comment-list > li'); | |
var commentsList = []; | |
var array = new Uint32Array(comments.length); | |
var randomIds = window.crypto.getRandomValues(array); | |
Array.prototype.forEach.call(comments, function(el, i){ | |
if (el.id) commentsList.push({id: el.id, rand: randomIds[i]}); | |
}); | |
commentsList.sort(function(a, b) { | |
return a.rand - b.rand; | |
}); | |
getWinnerComment = function(id) { | |
return document.querySelectorAll('#' + id + ' .comment-content p')[0].textContent; | |
} | |
console.log(commentsList[0].id, getWinnerComment(commentsList[0].id)); | |
console.log(commentsList[1].id, getWinnerComment(commentsList[1].id)); | |
console.log(commentsList[2].id, getWinnerComment(commentsList[2].id)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment