After refreshing in FireFox, the "disabled" attribute of the button fails to function

On my webpage, there are 2 buttons - one enabled by default and the other disabled. When I click on an enabled button, their states switch - the enabled button becomes disabled and vice versa. This functionality works perfectly in Chrome, Edge, and IE11, but there's an issue with Firefox (v 98.0.2).

Below is the code snippet:

<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>
  </head>
  <body>
      <button id="button1" onclick="$('#button1').prop('disabled',true);$('#button2').prop('disabled',false);">Button 1</button>
      <button id="button2"  onclick="$('#button2').prop('disabled',true);$('#button1').prop('disabled',false);" disabled>Button 2</button>
  </body>
</html>

The unexpected behavior observed in FireFox:

  1. Initially, everything functions as expected. The button states swap successfully. Here is the Inspector output when the page loads:
    <body>
          <button id="button1" onclick="$('#button1').prop('disabled',true);$('#button2').prop('disabled',false);">Button 1</button>
          <button id="button2" onclick="$('#button2').prop('disabled',true);$('#button1').prop('disabled',false);" disabled="">Button 2</button>
      
    
    </body>

(Apologies for not being able to upload an image)

  1. I click on "Button 1", it becomes disabled, and "Button 2" becomes enabled.
  2. Next, I refresh the page using F5 or the browser's refresh button.
  3. Both buttons revert to being enabled. The Inspector output when the page loads after refreshing:
<body>
      <button id="button1" onclick="$('#button1').prop('disabled',true);$('#button2').prop('disabled',false);">Button 1</button>
      <button id="button2" onclick="$('#button2').prop('disabled',true);$('#button1').prop('disabled',false);">Button 2</button>
  

</body>

It can be seen that the 'disabled' attribute of the second button is removed.

  1. Pressing Ctrl+F5 loads the page correctly with only one button enabled, functioning properly.

I have tried using disabled="disabled", but it does not solve the problem. Is this specific to FireFox or an error in the jQuery code? How can I fix or find a workaround for this issue?

Answer №1

When it comes to restoring user-entered state in Firefox, a common issue arises with disabled buttons. Despite being an ongoing problem for the past decade, a solution has yet to be found.

Check out this longstanding bug here

Answer №2

Understanding the Issue

Upon reviewing lotterfreiands answer, it became clear that the issue is related to a specific feature in FireFox that restores page state upon refresh.

A Practical Solution

To address this, I implemented an initialization within the $(document).ready() function. This workaround proved effective in resolving the FireFox behavior. It appears to be triggered with each F5-refresh following a restoration process. The code snippet utilized:

<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>
    <script type = "text/javascript">
        $(document).ready(function() {
            $('#button2').prop('disabled',true);
            $('#button1').prop('disabled',false);
        });
     </script>
  </head>
  <body>
      <button id="button1" onclick="$('#button1').prop('disabled',true);$('#button2').prop('disabled',false);">Button 1</button>
      <button id="button2"  onclick="$('#button2').prop('disabled',true);$('#button1').prop('disabled',false);" disabled>Button 2</button>
  </body>
</html>

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

Show the JSON response when making an AJAX request using the POST method

Can someone help me troubleshoot an issue with displaying JSON data using ajax? I'm not getting any results when I submit the form. Any suggestions would be appreciated. project_view.php <form id="formProjectsRepSearch" action="controller.php" me ...

Using QuerySelector with Angular Directive will throw an error as it is not a recognized

When creating a directive that integrates a jQuery carousel, I want to hide it at the beginning until it is fully ready. Here's the directive code snippet: return { restrict: 'E', replace: true, scope: true, templateUrl: ...

Evaluating Vue with AVA - The key is in the whitespace

Currently, I am engrossed in a Laracast Episode about Vue Testing with AVA. Everything has been going smoothly so far, but now I have encountered an intriguing bug. This is my notification.js: import test from 'ava'; import Notification from &ap ...

Tips for adding a class to the end of the DOM class

Greetings! I'm currently working with the code below: for ( let x: number = 0; x < this._vcr.element.nativeElement.querySelectorAll(".ui-steps-item").length; x++) { let className: any = this._vcr.element.nativeElement.querySelectorAll( ...

mongojs implementation that allows for asynchronous query execution without blocking

Forgive me for asking what may seem like a silly question, but I am struggling to make this work. Currently, as part of my learning journey with node.js and mongojs, I have encountered the following issue: Below is my server.js file server.get("/", funct ...

Implement JQuery UI Accordion to enhance navigation on mobile devices

I am implementing the JQuery UI Accordion to expand content on a website. Below is the basic markup structure: <div class="accordion"> <h2>Heading</h2> <div>Content</div> <h2>Heading</h2> <div& ...

What is preventing me from extracting all information from online shopping sites?

Recently, I've been tackling a project that involves scraping data from various e-commerce websites. However, I've encountered difficulties in extracting the desired information from these sites. For instance, when attempting to scrape all the li ...

Is it possible to have CSS handle only horizontal overflow?

Can CSS 2.1 be used to achieve horizontal overflow only? overflow: auto; If this code will result in both vertical and horizontal scrollbars for a block element, is there a way to display only horizontal scrollbars (for example, within a <div>)? Ho ...

Is there a method to prompt the browser to retain form data when using the back button?

Having a puzzling problem. I've developed a sophisticated quote form for our company that utilizes ajax to display prices for products based on user input. There are 6 hidden fields set up as follows: productname1 productname2 productname3 and the ...

Crafting artistic shapes using the Canny Edge Detection technique on Canvas

Can someone provide some guidance on using Canny Edge Detection to generate shapes in Canvas? ...

Using jQuery to dynamically format a date with variables

Looking to properly format a date for use in a compare function to sort data $(xml).find("item").each(function () { var dateText = $(this).find("Date").text(); var year = dateText.substr(0,4); var month = dateText.subst ...

Content within the p tag is failing to wrap onto separate lines

Take a look at the image below: Here is the code causing issues: <div class="excerpt"> <p>Manual for downloading and installing digital content from the Educational Canaima Project, comprised of learning resources that aim to promote interac ...

Using ng-click to manipulate variables within a list

In my ionic/angular/phonegap app, I have a list of items and I'm attempting to use an action sheet to pass in the variables. However, I am having trouble accessing the variable on the same line as the ng-repeater. Here is the markup: <ion-view vi ...

What is the best way to upgrade to a specific version of a child dependency within a module?

npm version: 7.24.2 Looking for assistance on updating a child dependency. The dependency in question is: vue-tel-input This dependency relies on libphonenumber-js with version ^1.9.6 I am aiming to update libphonenumber-js to version ^1.10.12. I have ...

Step-by-step guide to importing a directory in ReactJS

Is there a way to create an input that can upload an entire directory with all its folders intact? I attempted the following code: <input ref={wrapperRef} id="file-upload" name="file-upload" type="file" ...

Looking to find the video code in the page source and figure out how to get the video to play

I have a requirement where I must embed the video code directly in a blog post. After figuring out the necessary code for the video and saving it in a html file named video.html, I encountered an issue when trying to upload the file to the blog. Only the ...

Guide on inputting information into a dual-column table where one column is linked to the other

After successfully inserting data with hardcoded values to verify the relation between the 2 columns, I am now wondering if there is a way to reference the value of id in reply_id. This is how I manually inserted data: const { data, error } = await su ...

What is the method for implementing a dropdown box with select and non-select checkboxes, similar to the example shown in the image, using AngularJS?

https://i.sstatic.net/CTx8K.jpg I am seeking assistance in incorporating the image below into a dropdown menu using angularjs ...

retrieve information from a div using an AJAX request

I don't have much experience with ajax and javascript, but I need some assistance, so I will do my best to explain clearly! Below is the JavaScript associated with a button: $('#button-confirm').on('click', function() { $.ajax({ ...

Altering the input field's content in response to a button click through JavaScript's addEventListener function

I am struggling to get the input text from a form field to change when a button is clicked using JavaScript. I can't seem to figure out why it isn't working correctly. Any assistance would be greatly appreciated. <!doctype html> & ...