I’m looking to use jQuery to load Divs with random margins effecting the css. Below is the script I’m attempting use, trying to create a number random from 1-500, and loading that as the margin. But I’m not a jQuery wiz, and I’m missing something.
Thanks so much for the help!
<script type="text/javascript">
$(function(){
$("#randomnumber").load(function() {
var numRand = Math.floor(Math.random()*501);
$("#randomnumber").css("marginRight",numRand);
});
});
</script>
<div id="page-wrap">
<div id="randomnumber">BIG BOY</div>
</div>
The problem is there is no
.load()event for this element, at least not one that’ll won’t fire on load, you need a.each()like this:You can test that here, or since you’re doing one element, make it simpler like this:
You can test that version here.