hover over the box, it should change its position randomly remove jquery without affecting code

Viewed 164

I have done this far but the code isn't working. It's working in fiddle but here it isn't. I am attaching a solution which I want and the code as far as I have done. I have somehow figured out that with jQuery it's working but I don't want to use jQuery or tell me how to use jQuery in sublime text.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Catch Me</title>
<style>
button {
    background-color: rgb(228, 6, 248);
    border: none;
    color: white;
    padding: 20px 20px;
    text-align: center;
    position: relative;
    top: 100px;
    left: 100px;
}
</style>
</head>
<body>
    <div class="main">
    <button name="button">Catch Me</button>
    </div>
    
</body>
</html>
<script type="text/javascript">
$(function(){
    $("button").on({
        mouseover:function(){
            $(this).css({
                left:(Math.random()*200)+"px",
                top:(Math.random()*200)+"px",
            });
        }
    });
});
</script>
The solution I want is this: https://ninjasfiles.s3.amazonaws.com/0000000000001878.gif

1 Answers

Please try this:


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Catch Me</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<style>
button {
    background-color: rgb(228, 6, 248);
    border: none;
    color: white;
    padding: 20px 20px;
    text-align: center;
    position: relative;
    top: 100px;
    left: 100px;
}
</style>
</head>
<body>
    <div class="main">
    <button name="button">Catch Me</button>
    </div>


<script type="text/javascript">
    $(document).ready(function(){
        $("button").mouseover(function() {
            $(this).css("left", "" + (Math.random()*200) + "px");
            $(this).css("top", "" + (Math.random()*200) + "px");
        });
    });
</script>
    
</body>
</html>

Related