Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

245
Views
I am trying to use javascript to double the size of one image

My html do contains 3 images all the same size. I am trying to write some javascript so the third image the Jamaican flag I want to double went I move the mouse over it. I thought this code I wrote would work but it does not

<!DOCTYPE html>

<html lang="en">
<head>
<title>Task 1</title>
</head>
<body>
<img src="usa.gif" height="200" width="250"/>
<img src="mexico.gif" height="200" width="250"/>
<img id = "jamaica"height="200" width="250"onMouseover = "setNewImage()" onMouseout = 
"returnOldImage()" src="jamaica.gif"/>

<script>
    var myImages = [
        "usa.gif",
        "canada.gif",
        "jamaica.gif",

    ];

    function setNewImage()
    {
        document.getElementById("jamaica").src="jamaica.gif" height="400" 
 width="500";
    }
    function returnOldImage()
    {
      document.getElementById("jamaica").src="jamaica.gif" height="200" width="250";
    }



</script>
</body>
</html>

Any help is always greatly appreciated Thanks

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

It would be better to use CSS to transform the image on hover. In the code snippet I've added a class to the li where the jamaica flag is enlarged on hover. If you want any flag to enlarge on hover, just remove the jamaica class, and change the css from ul li.jamaica a:hover img to just ul li a:hover img.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Transform with CSS</title>
<style>
    ul {
        padding: 0;
        margin: 50px 20px;
        list-style: none;
    }
    ul li {
        margin: 5px;
        display: inline-block;
    }
    ul li a {
        padding: 5px;
        display: inline-block;      
        border: 1px solid #f2f2f2;
    }
    ul li a img {
        width: 125px;
        height: 125px;
        display: block;
    }
    ul li.jamaica a:hover img {
        transform: scale(1.5);
        box-shadow: 0 0 10px rgba(0, 0, 0, 0.5);
    }
</style>
</head>
<body>
    <ul>
        <li><a href="#"><img src="https://cdn.countryflags.com/thumbs/united-states-of-america/flag-square-250.png" alt="United States"></a></li>
        <li><a href="#"><img src="https://cdn.countryflags.com/thumbs/canada/flag-square-250.png" alt="Canada"></a></li>
        <li class="jamaica"><a href="#"><img src="https://cdn.countryflags.com/thumbs/jamaica/flag-square-250.png" alt="Jamaica"></a></li>
    </ul>
</body>
</html>

If you have to use JavaScript instead of CSS do this:

<!DOCTYPE html>

<html lang="en">
<head>
<title>Task 1</title>
</head>
<body>
<img src="https://cdn.countryflags.com/thumbs/united-states-of-america/flag-square-250.png" height="200" width="250"/>
<img src="https://cdn.countryflags.com/thumbs/canada/flag-square-250.png" height="200" width="250"/>
<img src="https://cdn.countryflags.com/thumbs/jamaica/flag-square-250.png" id="jamaica" height="200" width="250" onMouseover="setNewImage()" onMouseout ="returnOldImage()"/>

<script>
    var myImages = [
        "usa.gif",
        "canada.gif",
        "jamaica.gif",

    ];

    function setNewImage()
    {
        var jamImg = document.getElementById("jamaica");
        jamImg.style.height="400px";
        jamImg.style.width="500px";
    }
    function returnOldImage()
    {
        var jamImg = document.getElementById("jamaica");
        jamImg.style.height="200px";
        jamImg.style.width="250px";
    }

</script>
</body>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!