The expected result is not being returned when clicking a jQuery button

Below is the jQuery code snippet that I am using:

<!DOCTYPE html>
<html>
<head>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
  <script>
    $(document).ready(function() {
      $("#button1").click(function() {
        alert(1)
        var div = $("div");
        div.animate({
          left: '100px'
        }, "slow");
        div.animate({
          fontSize: '3em'
        }, "slow");
      });

      $("button").click(function() {
        alert(3)
        var div = $("div");
        div.animate({
          left: '50px'
        }, "slow");
        div.animate({
          fontSize: '7em'
        }, "slow");
      });
    });
  </script>
</head>
<body>
  <button id="button2">Start Animation2</button>
  <button id="button1">Start Animation</button>
  <p>By default, all HTML elements have a static position, and cannot be moved. To manipulate the position, remember to first set the CSS position property of the element to relative, fixed, or absolute!</p>
  <div style="background:#98bf21;height:100px;width:200px;position:absolute;">HELLO</div>
</body>
</html>

When I execute the above code, the Start Animation2 button works only for the first time and has no effect on subsequent clicks. However,

the Start Animation

button functions correctly on every click. It seems that the issue lies in the event listener for the

Start Animation2

button not being triggered multiple times. Why does this happen?

Answer №1

The issue arises from the selectors used. Events have been assigned to both #button1 and button. Consequently, when #button1 is clicked, both event handlers are triggered. On the other hand, clicking on #button2 only activates the event handler attached to button. This may seem ineffective because the final position of #button1 results in everything being stationary - hence no observable movement takes place.

Answer №2

The reason for this behavior is

When you use the following code:
div.animate({
    left: '100px'
}, "slow");`

it will assign a value of 100px to your left property and not increment it incrementally. The property gets set once, and subsequent iterations won't have any effect as it remains at 100px.

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

Display script based on inner width - avoiding the use of "potentially harmful request.form"

What I'm Seeking: In order to display affiliate ads on my responsive website, I need to load different javascripts based on the innerwidth property using jQuery. However, attempting to load these scripts in hidden fields and dynamically swapping them ...

Troubles with Fixed Navigation Bar in JavaScript-Enabled Scrolling

I've implemented a JavaScript-enabled scrolling navigation bar that starts below a hero graphic and then sticks to the top when it reaches the top of the page. The functionality works as intended, but there is one issue - when the navigation bar reach ...

Steps to troubleshoot a scrollbar problem while applying flexbox in CSS

I'm currently working with a front-end framework that utilizes a flex layout. In my development of an admin page, I have included sections for the sidebar, header, and main. My objective is to ensure that the sidebar and the header + main sections fun ...

Leveraging Font Awesome with Google's Autocomplete in Pac-Input

I'm experimenting with using the Font Awesome Map Marker in a Google Autocomplete Input. My aim is to position the Map Marker to the left of the placeholder text, apply color only to the map marker, and ensure that it remains visible even while the us ...

Locate a specific element in the HTML Source code, then update the variable value with a different value

Imagine this code snippet embedded in the webpage: <script type="text/javascript"><-- SOMEVAR = "SOMEVAL"; SOMEVAR2 = "SOMEVAL2"; SOMEVAR3 = VAL; SOMEVAR4 = VAL; //--> </script> <script type="text/javascript" src="http://somesource.sr ...

Refreshing content with Ajax when the back button is clicked

Hey everyone, I've been working on an ajax site and I'm having trouble getting the content to reload when the back button is clicked. I'm using Dynamic Drives ajax content script along with a script that changes the URL onclick and a popstat ...

Having difficulty in animating the upward movement of textbox2

I've got a form that contains 2 buttons and 2 textareas. When the form loads, I want to display only section1 (button, textarea) and the section2 button. Upon clicking the section2 button, my aim is to hide the section1 textarea, reveal the section2 t ...

What causes my Bootstrap 5 popover to no longer open on focus when I reopen the modal it is attached to?

I am facing an issue with my Bootstrap 5 modal and the popovers that are attached to it. On hovering over a button in my HTML, a popover appears asking 'Are you sure?' and instructing the user to click the button to proceed. Clicking the button ...

SEO Friendly URL for JQuery Tabbed Content

I have developed a jQuery powered tabbed content system, where clicking on a tab changes the content below. However, I am facing an SEO issue. The SEO specialist has advised me to make it more SEO-friendly by adding different URLs to each tab. Can anyone ...

Does the sliding transition between views function properly, even though the view appears on top of the header and behind the footer

My angular app is running smoothly, but now I want to add animations between views. When a user clicks on a link in the navbar, I want the view to slide to a different one. I was inspired by this example: Sliding between views. However, when I implemented ...

Organizing table data by columns in a continuous loop

I'm currently working on extracting data from a database, organizing it into tables (potentially multiple tables), and presenting them in columns of 4 across several pages. My main concern is figuring out how to display the tables horizontally like t ...

Internet Explorer experiences performance issues when a table containing over 500 rows is being utilized

Can anyone offer advice on speeding up the display of large tables with 500+ rows in Internet Explorer? Here is my current approach: The MySQL query result includes 500+ rows, which I then loop through using a while loop to display: echo "<tr class=& ...

Using Two JavaScript Functions with Identical Names in One HTML File

I am currently facing an issue with a function where the data is retrieved from the getValue() function. The following code snippet is a fragment. grid.js function gridLoadComplete(){ var data = getValue(); } Take into account the following H ...

Discovering the smallest and largest values in an object literal using JavaScript

I am looking to extract the minimum value from an object literal and utilize it in AngularJS, as shown below: scope.data = { 'studentName' : "Anand", 'socialStudies' : "98", 'english' : "90", 'math' ...

I can't figure out why this form isn't triggering the JS function. I'm attempting to create an autocomplete form field that connects to a MySQL database using a PHP script and AJAX

I am encountering an issue while trying to implement the .autocomplete() function from jQuery UI with a list of usernames fetched from a MySQL database using a PHP script. Strangely, it is not functioning as expected and no errors are being displayed in th ...

Unable to get CSS transition to function properly within Chrome Extension

I am currently attempting to create an effect where the word "credits" expands to reveal the credits when hovered on in a Chrome Extension. However, I am encountering difficulties in getting it to work properly. Here is the CSS code I have used for hand ...

Clicking on the submit button in asp.net triggers the removal of the dropdown list items that were dynamically populated using a jQuery ajax request

I am struggling to populate a select box in asp.net using a jQuery ajax call. The value briefly appears when the submit button is clicked, but then disappears after the page refreshes. It seems to work fine with <input type="button">, but I am lookin ...

How to choose an item in Jquery using its exact location?

For my testing purposes, I rely on Selenium Webdriver to ensure the functionality of creating a new time slot in a calendar. The specific calendar I am working with is the Jquery week calendar which can be found at the following link: https://github.com/th ...

Is there a way to activate an event once an image has been loaded using lazy loading?

I have images that need to be positioned at the center of their parent div using absolute positioning. I already have code that accomplishes this task. Recently, I integrated the Lazy Load plugin which is functioning properly. However, I am facing a chall ...

How can I effectively design a vertical table with a split background for optimal visual appeal?

Is there a way to creatively present this data with a split background using html and css? https://i.sstatic.net/qsCOM.png ...