When clicking on the button, I am assigning random margins from the top and left to it, so it can move but it’s not working.
HTML
<button id="bouncing" class="btn btn-primary">Click Me</button>
JS
var button = document.querySelector("button");
button.addEventListener("click", function() {
var changeTop = (Math.random() * ($(window).height() - $("button").height()));
var changeLeft = (Math.random() * ($(window).width() - $("button").width()));
$("button").css("margin-top", changeTop + "px");
$("button").css("margin-left", changeLeft + "px");
});
2
Answers
You forgot to add jquery. Add it via CDN or install it and it will work, like this-
This works fine.You probably didn’t include jQuery in your code