The table toggle feature seems to be malfunctioning in Safari, whereas it works perfectly in Chrome

My table includes a td element that serves as a toggle switch, transitioning between 3 states flawlessly in Chrome. However, I am facing issues with its functionality in Safari and seek assistance in rectifying the issue to ensure cross-browser compatibility.

Attached are both an image and the relevant code snippet.

Grateful for any insights or solutions shared.

M.

highlight {
    background-color: #86C440;
    color: white;
}

th {
  font-size: 30px;
}

tr {
  text-align: left;
}

td {
  font-size: 20px;
  background-color: #d4d6d3;
  cursor: pointer;
}

.center {
  margin-left: auto;
  margin-right: auto;
}

tr {
  position:relative;
  transform:scale(1,1);
}

td.last{
  position:fixed;
  border: 5px solid #d4d6d3;
  background-color: #4eafef;
  left: -46px;
  top: -46px;
  height: 7px;
  width: 7px;
  line-height: 7px;
  cursor: pointer;
}
    <html>
        
    <body>
<div id="switch">1</div>
<br><br><br><br><br><br><br>

<style>

  table          {border-collapse: collapse; border: 5px solid white; padding: 5px;}
  table td       {text-align: center; color: black; border: 5px solid white; padding: 10px; height: 66px; width: 66px;}
  
</style>


<table class="center">
 <tr>
   <td>1</td>
   <td>2</td>
   <td id="toggle" class="last" onclick="toggle1()"></td>
 </tr>
    <tr>
   <td>3</td>
   <td>4</td>
 </tr>
    <tr>
   <td>5</td>
   <td>6</td>
 </tr>
</table>

</body>

    <script>

    function toggle1() {

                var key = document.getElementById("switch").innerHTML;

              if (key == 1) {

                      document.getElementById("toggle").style.backgroundColor = "white";
                      document.getElementById("toggle").style.backgroundImage = "url('')";
                      document.getElementById("toggle").style.borderStyle = "solid";
                      document.getElementById("toggle").style.borderColor = "#d4d6d3";
                      document.getElementById("toggle").style.borderWidth = "5px";
                      document.getElementById("switch").innerHTML = 2;

                            //console.log("toggle clicked 1st time!");

              } else if (key == 2) {

                      document.getElementById("toggle").style.backgroundColor = "#a370f0";
                      document.getElementById("toggle").style.backgroundImage = "url('')";
                      document.getElementById("toggle").style.borderStyle = "solid";
                      document.getElementById("toggle").style.borderColor = "#d4d6d3";
                      document.getElementById("toggle").style.borderWidth = "5px";
                      document.getElementById("switch").innerHTML = 3;

                            //console.log("toggle clicked 2nd time!");

              } else if (key == 3) {

                      document.getElementById("toggle").style.backgroundColor = "#4eafef";
                      document.getElementById("toggle").style.backgroundImage = "url('')";
                      document.getElementById("toggle").style.borderStyle = "solid";
                      document.getElementById("toggle").style.borderColor = "#d4d6d3";
                      document.getElementById("toggle").style.borderWidth = "5px";
                      document.getElementById("switch").innerHTML = 1;

                            //console.log("toggle clicked 3rd time!");

              }

    }

    </script>

    </html>

Answer №1

When comparing the behavior of Chrome/Edge to Safari, there are several issues that may lead to differences.

One problem is the presence of a style declaration within the body element instead of the head element, which can be flagged as invalid HTML by an HTML validator. The solution provided below moves this style declaration to the head element.

Another issue involves a setting for tr elements that applies a transform, causing Chrome to position the fixed element relative to the table's beginning rather than relative to the viewport. While I do not have a complete explanation for this behavior, it seems that any kind of transform triggers this change and creates a new stacking context.

A warning from the validator arises due to a third cell in the first row but not in subsequent rows. It is unclear how the table layout should appear under these circumstances, especially considering the fixed setting that visually removes the third cell from the flow. To address this, the snippet removes the third cell and adds a button before the table, resulting in consistent behavior across Safari and Chrome/Edge.

Adjustments might be needed for the button's positioning – one potential solution is placing the button and the table inside a container with specified positioning based on the desired layout requirements.

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

I'm experiencing some unusual behavior with the Windows media app when using HTML and CSS

I have a Windows Media Player box on my page, but it overlaps every piece of HTML code. How can I get it to move to the back so that I can still use it? Another problem is that everyone who visits my page needs a plugin to load it, even though most people ...

Creating interactive labels in a histogram that update based on the user's input

Currently, I have operational code in place that takes input data and generates a histogram based on set thresholds. When the code is executed, the histogram changes dynamically as the threshold slider is adjusted. However, there seems to be an issue wit ...

Combining MarkDown elements into a DIV or a custom HTML tag

I have utilized the Jeykll tool to convert mark down content into HTML. I am looking to group the following mark down elements within a div element or another custom HTML tag. Markdown #Multiple Axis {:.title} Different types of data can be vis ...

Execute HTML code within a text field

Is it possible to run html code with javascript inside a text box, similar to w3schools.com? I am working solely with html and javascript. Thank you. For example, I have two text areas - one for inserting html, clicking a button to run the code, and displ ...

What is the process of uploading a video and showcasing it on an HTML page?

I have successfully uploaded images and displayed them on an HTML page. Now, I am looking to do the same for videos. Here is my current code: $('#addPhotosBtn').click(function() { $(this).parents().find('#addPhotosInput').click(); }) ...

Can a webpage be sent to a particular Chromecast device without using the extension through programming?

My organization has strategically placed multiple Chromecasts across our facility, each dedicated to displaying a different webpage based on its location. Within my database, I maintain a record of the Chromecast names and their corresponding URLs. These d ...

Chakra UI Not Displaying Proper Responsiveness on the User Interface

I recently integrated Chakra UI for styling a project on Next.js, and while most components are functioning properly, I am facing challenges with implementing responsive layouts. The styles do not seem to adapt when the screen size is reduced. Here's ...

Interactive Infographics in HTML5 format

Currently, I am unable to find any examples on how to accomplish a specific task mentioned in the following link. If you have a tutorial with step-by-step instructions or a code project available, it would greatly assist me. Link: Evolution of WEB ...

Show a pop-up notification for a compact disc

Received a CD containing various tutorials created in HTML. I am looking to have a browser window open with specific parameters, such as no toolbars and fixed width/height, to properly display the content. Using window.open with multiple parameters trigge ...

Working with AngularJS: binding data to dynamically appended HTML elements using JavaScript

Is there a way to connect an angular event and model to an html element that is added through javascript code? To see my code, click here: https://jsfiddle.net/hq7qk48n/13/ <div ng-app> <div ng-controller="MyController"> <input ...

Sharing details of html elements using ng-click (AngularJS)

I am currently exploring options to enable users to click on a "open in new tab" link, which would essentially transfer that HTML element into a fresh window for their convenience. I am seeking advice on how to achieve this. At the moment, I am able to la ...

Unable to properly zoom in on an image within an iframe in Internet Explorer and Google Chrome

My image zoom functionality works perfectly on all browsers except for IE and Google Chrome when opened inside an iframe. Strangely, it still functions flawlessly in Firefox. How can I resolve this frustrating issue? The image link was sourced from the i ...

Experiencing an issue where the canvas element fails to render on mobile Chrome browser,

I've encountered an issue with a script that draws a canvas based on the background color of an image. The image is loaded dynamically from a database using PHP. The responsive functionality works fine on mobile Safari, but not on Chrome. When the re ...

Storing HTML table data in a MySQL database will help you

Operating a website focused on financial planning where users can input various values and cell colors into an HTML table. It is crucial to uphold the integrity of these HTML tables. How can I store the complete HTML table (including values and colors) i ...

Update the chosen option of the <select> element that was generated through PHP if there is a parameter value present in the URL

I'm currently working on a code snippet like this: <select name="id" id="id"> <OPTION VALUE="null">Select<OPTION> <? foreach ($genres as $row){ ?> <OPTION VALUE="<? echo $row->id; ?>"><? echo ...

Troubleshooting code: JavaScript not functioning properly with CSS

I am attempting to create a vertical header using JavaScript, CSS, and HTML. However, I am facing an issue with the header height not dynamically adjusting. I believe there might be an error in how I am calling JSS. Code : <style> table, tr, td, t ...

Which file is the optimal placement for the Bootstrap directory: 'header.php' or 'function.php'?

Having dabbled in WordPress Theme templates for a while, I've decided to try my hand at creating a theme from scratch. After uploading the Bootstrap features onto my server, I'm pondering whether it's better to call the Bootstrap.css files ...

Steer clear of downloading images while on your smartphone

As I develop a responsive website, I encounter the challenge of optimizing image loading based on viewport size. While using CSS to hide images not needed for certain viewports can reduce display clutter, it does not prevent those images from being downloa ...

Utilizing jquery and ajax to submit a form seamlessly without the need to refresh the web page

I've been struggling to submit a form without refreshing the entire page. I integrated some source code that I found, but for some reason, it's not working! Here is the HTML form I'm trying to submit: <form method="POST" action="" name= ...

Circular picture contained within a Bootstrap column on an irregularly-shaped image file

I am looking to create a circular effect for an image that is originally rectangular, resembling a typical profile picture. I have come across several sources on how to do this. The challenge arises in trying to achieve this effect within a bootstrap colu ...