1

I need help converting a Jquery code into a javascript code where it changes the border radius of a div when hovering over it.

HTML
      <!DOCTYPE html>
    <html>
    <head>
        <link rel="stylesheet" href="SampleQ3.css" />
    </head>
    <body>
        <div>
            <h4>Pasta</h4>
        </div>
        <script src="SampleJavaScript.js"></script>
    </body>
    </html>
   


JS
    $(document).ready(function () {
        $("div").hover(function () {
            $(this).css("border-radius", "0%");
    
        }, function () {
            $(this).css("border-radius", "200px 200px 200px 200px");
        });
    });
3
  • jQuery is already in Javascript code, I guess what you ask is to have the equivalent in DOM. Commented Nov 4, 2021 at 10:26
  • Still new to this not sure what DOM means, basically just want to know how to convert the java code into jquery or vice-versa. Commented Nov 4, 2021 at 11:09
  • you can do this only using css. Commented Nov 4, 2021 at 12:05

1 Answer 1

1

You can do all of that only using css, but I will provide both solution js and css.

javascript code:

let divs = document.getElementsByTagName("div");
for(div of divs) {
   div.addEventListener("mouseenter", function( event ) {
       event.target.style.borderRadius = 0%;
   }, false);

   div.addEventListener("mouseover", function( event ) {
       event.target.style.borderRadius = '200px';
   }, false);

}

To do the same with css you can add a class to make it easer to target.

// css 
<style>
.div-round {
    border-radius: 200px;
}

.div-round:hover {
  border-radius: 0%;
}

</style>

//add class to div
<div class='div-round'>...</div>
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, for the css part you did, is there a way so we dont edit css and make it so javascript adds the border-radius 0% to the div hover section without touching css?
These are two ways that I would do it, they are separated ways. @helpme7
From the documentation: The .hover() method binds handlers for both mouseenter and mouseleave events. ... shorthand for $( selector ).mouseenter( handlerIn ).mouseleave( handlerOut );. Which is what this javascript code is doing. Although for something simple like this, I would recommend the css option. And also you could simplify 0% to just 0.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.