What is preventing the button inside my div element from responding?

I created a button inside of a moving div with a background image and added an animation to the div for movement. However, I encountered an issue where the button did not react as expected. To test this, I added another stationary button which reacted accordingly. Can anyone assist me in resolving this problem?

Below are the relevant code snippets:

HTML

<h1>Animated Aquarium</h1>
<div id="Aquarium">
  <div id="box1">
    <button type="button" id="button1" onclick="jelly fish()">  
      <img src="/img/7959f86c-dccb-410e-b928-73a282b866f1/jellyfish.png"id="jellyfish">
    </button> 
  </div>
  <button type="button" id="button1" onclick="jellyfish()">test</button>
</div>

Style

body {
  background: linear-gradient(to bottom right, #2F80ED, #56CCF2);
  height: 100vh;
}
#Aquarium {
  width: 700px;
  height: 500px;
  border: 10px solid #000;
  margin: 0 auto;
  background-size: cover;
  background-repeat: no-repeat;
  background-image: url("https://ak4.picdn.net/shutterstock/videos/9874064/thumb/1.jpg");    
}
#box1{
  animation: jellyfishswim 24s infinite;  
  position: absolute;
}
#button1{    
  background-color: inherit;
  border:0px;
}
#jellyfish{
  width: 100px;
  height: 100px;
}

@keyframes jellyfishswim {
  /* Keyframes details here */
}

@keyframes giantjellyfishswim {
  /* Keyframes details here */
}

Script

function jellyfish() {
  document.getElementById("jellyfish").style.width = "300px";
  document.getElementById("box1").style.animation = "giantjellyfishswim 12s infinite";
  alert("You distory the aquarium");
}

Answer №1

Modify

<button type="button" id="button1" onclick="jelly fish()">  

into

<button type="button" id="button1" onclick="jellyfish()">  

function jellyfish() {
    document.getElementById("jellyfish").style.width = "300px";
    document.getElementById("box1").style.animation = "giantjellyfishswim 12s infinite";
    alert("You distory the aquarium");
}
body {
  background: linear-gradient(to bottom right, #2F80ED, #56CCF2);
  height: 100vh;
}

#Aquarium {
  width: 700px;
  height: 500px;
  border: 10px solid #000;
  margin: 0 auto;
  background-size: cover;
  background-repeat: no-repeat;
  background-image: url("https://ak4.picdn.net/shutterstock/videos/9874064/thumb/1.jpg");
}

#box1 {
  animation: jellyfishswim 24s infinite;
  position: absolute;
}

#button1 {
  background-color: inherit;
  border: 0px;
}

#jellyfish {
  width: 100px;
  height: 100px;
}



@keyframes jellyfishswim {
  0% {
    margin-left: 0px;
    transform: scaleX(-1);
  }

  10% {
    margin-left: 140px;
    margin-bottom: 100px;
    transform: scaleX(-1);
  }

  20% {
    margin-left: 280px;
    margin-bottom: 200px;
    transform: scaleX(-1);
  }

  30% {
    margin-left: 420px;
    margin-bottom: 300px;
    transform: scaleX(-1);
  }

  40% {
    margin-left: 560px;
    margin-top: 400px;
    transform: scaleX(-1);
  }

  50% {
    margin-left: 600px;
    margin-top: 420px;
    transform: scaleX(1);
  }

  60% {
    margin-left: 500px;
    margin-bottom: 340px;
    transform: scaleX(1);
  }

  70% {
    margin-left: 420px;
    margin-bottom: 300px;
    transform: scaleX(1);
  }

  80% {
    margin-left: 280px;
    margin-bottom: 200px;
    transform: scaleX(1);
  }

  90% {
    margin-left: 120px;
    margin-bottom: 150px;
    transform: scaleX(1);
  }
}


@keyframes giantjellyfishswim {
  0% {
    margin-left: 0px;
    transform: scaleX(-1);
  }

  10% {
    margin-left: 70px;
    margin-bottom: 50px;
    transform: scaleX(-1);
  }

  20% {
    margin-left: 140px;
    margin-bottom: 100px;
    transform: scaleX(-1);
  }

  30% {
    margin-left: 210px;
    margin-bottom: 150px;
    transform: scaleX(-1);
  }

  40% {
    margin-left: 280px;
    margin-top: 200px;
    transform: scaleX(-1);
  }

  50% {
    margin-left: 300px;
    margin-top: 210px;
    transform: scaleX(1);
  }

  60% {
    margin-left: 250px;
    margin-bottom: 170px;
    transform: scaleX(1);
  }

  70% {
    margin-left: 210px;
    margin-bottom: 150px;
    transform: scaleX(1);
  }

  80% {
    margin-left: 140px;
    margin-bottom: 100px;
    transform: scaleX(1);
  }

  90% {
    margin-left: 70px;
    margin-bottom: 50px;
    transform: scaleX(1);
  }
}
<div id="aquarium">
  <div id="box1">
    <button type="button" id="button1" onclick="jellyfish()">  
      <img src="/img/7959f86c-dccb-410e-b928-73a282b866f1/jellyfish.png" id="jellyfish">
    </button>
  </div>
</div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Modifying KineticJs.Image with css styling

Hey there, I'm currently working on a project that involves canvas manipulation. I've successfully figured out how to draw an image and move it within the canvas, which wasn't too difficult to achieve. However, I'm now facing a challeng ...

Tips for using parsley on a dynamically loaded webpage

I'm encountering an issue with applying validation for a datepicker in AJAX loaded content. The validation doesn't seem to work for the loaded content. Can someone please assist me with this? /Script/ function applyValidationForDatepicker(feest ...

Adjust the color of the font icon within a button when hovering over it

On the bottom of my website (still in development) , there are 3 buttons with an arrow icon preceding the text. The arrow is created using an icon font and I would like it to change color (along with the button text) when hovered over. Any suggestions on ...

Issue encountered while trying to exhibit jpg image content from server side on html

I am attempting to display an image from server-side data using an Ajax call. The data stored on the server looks something like this: "ÿØÿàJFIFÿÛC4­¶¸UOHlʵcBò)3¹_È'I4~&éüÿ§Ú<ã©ã4>'Øù¿ÇÄmËCÈgÚsZ¥ë" Upo ...

How can I show the remaining paragraph text upon clicking a button in a responsive manner using CSS and JavaScript?

I want to add a nice animation effect to the height of my text when a button is clicked. I've managed to achieve this by setting a height: 30px with overflow: hidden initially, and then toggling a class with height: 300px. However, I'm facing an ...

Arrange a variety of images with different dimensions in a narrow row while keeping the width fixed and adjusting the height accordingly

Imagine you have a collection of 3 (or more) images, each with different sizes and aspect ratios, within a fixed-width div (for example width:100%): <div class="image-row"> <img src="1.png"> <img src="2.png"> <img src="3.png" ...

Experiencing a problem with the 'circle-color' data-driven styling feature in Mapboxgl.js & GeoJSON, yet everything is functioning correctly with the default color

I have been attempting to generate a map, plot data points using GeoJSON and Mapbox. Everything goes smoothly when I use a default color code for all points, but as soon as I try to implement data driven styling to assign different colors based on differen ...

The function within the loop will only execute one time

I am facing an issue with a JavaScript function that is looping through my data. I have another function called inside the loop, but it only executes once on the last index. The code looks like this and I want this function to execute every time. Insid ...

Adjust the ng-show attribute in a different controller

I am attempting to toggle the visibility of a div using ng-show. Specifically, I have a navbar that should only be displayed in certain views. I have one controller managing the behavior of this div, and another controller where I want to manipulate the v ...

Use vue.js to add a block of content after every sixth iteration in a loop

Currently, I have a list of offer cards rendering through a loop. I am adding a row div every 3rd column (bootstrap) element. Now, I need to also add another column element (banner block) for every 6th element in order to achieve a layout like the one show ...

Equal-Height Slide Deck Holder

I'm struggling to maintain a consistent height of 700px for my image slideshow, which consists of four images with varying heights. I attempted setting the img at max-height: 700px, but unfortunately that didn't yield the desired outcome. You ca ...

Issue with date: date.toLocaleTimeString function is invalid

I'm currently working on creating a time display component using hooks, but I'm running into an issue with the error message "TypeError: date.toLocaleTimeString is not a function." As a newcomer to this technology, any guidance would be greatly a ...

Functionality of local data storage

I'm currently exploring the capabilities of the localStorage feature. Let's say I have a large JSON object stored using localStorage. I need to share this data with the testing team for review. However, if I try to display the stored data on an H ...

Disconnecting socket when A-tag with Href is clicked

Currently, I am working on a project that involves using socket.io. In this project, I need to provide users with links to download files. <a href="<path>" >Link Name</a> Interestingly, whenever I click on the link to download the file, ...

Utilizing a custom font to emphasize the extended phrase in the following sentence

I've been trying to use word-wrap to break long words into the next line, but unfortunately it's not working as expected. You can view my JsFiddle code for reference. The divs on my page are generated dynamically, and here is an overview of what ...

Dealing with callback errors: error handling is anticipated

In my Vue web application, I have enabled ESLint and encountered an issue with the following code: myApi.get('products/12').then((prodResponse) => { state.commit('ADD_PRODUCT', {product: prodResponse.data}) }, error => { cons ...

Creating customized JavaScript bundles with TypeScript - a beginner's guide

Our ASP.NET MVC application contains a significant amount of JavaScript code. To optimize the amount of unnecessary JS being loaded to the browser, we utilize the BundleConfig.cs file to create multiple bundles. On the View, we implement logic to determin ...

Different div elements are clashing with each other when it comes to hiding and

Having added multiple div elements with different IDs, I am facing an issue where clicking one div displays it while hiding the others. I am looking for a solution to prevent conflicts between the divs. Any suggestions on what might be causing this problem ...

Launching a React project using the terminal - my step-by-step process

I'm attempting to initiate a new react-app utilizing the command npx create-react-app <app name> as shown below: npx create-react-app new-app However, after downloading, it seems to be stuck. It's not progressing at all. I've even rei ...

Can the background image be resized while preserving the offsets?

Can I adjust the offsets of multiple images within an image and resize it to a different width and height? I have an image that I want to shrink while maintaining the x and y offsets of the individual images inside it. Below is a sample of the image I ha ...