Why won't my JavaScript function properly in the Firefox browser?

I've created a script to change the background color of a checkbox when it's checked, but unfortunately, it doesn't work on Mozilla.

<script type="text/javascript">
function checkBoxList1OnCheck(listControlRef)
{
 var inputItemArray = listControlRef.getElementsByTagName('input');

 for (var i=0; i<inputItemArray.length; i++)
 {
  var inputItem = inputItemArray[i];

  if ( inputItem.checked )
  {
   inputItem.parentElement.style.backgroundColor = 'Red';
  }
  else
  {
   inputItem.parentElement.style.backgroundColor = 'White';
  }
 }
}
</script>

<form id="form1" runat="server">
    <div>
    <asp:CheckBoxList id="CheckBoxList1" onclick="checkBoxList1OnCheck(this);" runat="server">
  <asp:listitem value="1">Item 1</asp:listitem>
  <asp:listitem value="2">Item 2</asp:listitem>
  <asp:listitem value="3">Item 3</asp:listitem>
 </asp:CheckBoxList>
    </div>
    </form>

and even in page load i add as follows

CheckBoxList1.Attributes.Add("onclick", "checkBoxList1OnCheck(this);");

But still i am unable to work it out in Mozila can any one help me

Answer №1

Quick tip: To troubleshoot JavaScript errors, open the console by pressing CTRL + SHIFT + j. Note that the parentElement property is not compatible with Mozilla browsers.

Consider using parentNode as an alternative:

        for (var x = 0; x < elementsArray.length; x++) {
            var currentElement = elementsArray[x];

            if (currentElement.checked) {
                //currentElement.parentElement.style.backgroundColor = 'Blue';//May not work in Mozilla
                currentElement.parentNode.style.backgroundColor = 'Blue';
            }
            else {
                //currentElement.parentElement.style.backgroundColor = 'Green';//May not work in Mozilla
                currentElement.parentNode.style.backgroundColor = 'Green';

            }

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

Is it possible to submit a form through a JavaScript hotkey?

Here's the current code that I'm working with: <select tabindex="2" id="resolvedformsel" name="resolved"> <option selected="selected" value="yes">resolved</option> <option value="no">not resolved</option> ...

Using jQuery to parse an entire HTML page

When I use ajax to load an HTML file, I am trying to place the result into a jQuery object. However, when I attempt this, it seems to return null. Can someone guide me on how to achieve this? The content I am loading includes doctype, head elements, and bo ...

Maximizing the potential of asp.net and javascript when dealing with multiple fields and the onchange

My asp.net webpage is filled with numerous textboxes, each triggering a JavaScript function on the onchange event. The script changes a textbox to 'Y' to indicate which section of the page has been altered. While everything functions correctly, ...

Cannot access array method(s) using MyObject.prototype.reduce callback

As I delve into prototyping, I encountered an issue with using forEach and reduce on my ArraySet prototype. The arrow function callback works well with forEach, but I hit a roadblock with reduce. It seems the notation that works for a normal Array doesn&ap ...

Finding the most recent instance of a deeply buried value based on a specific property

I have an example object: const obj = { group: { data: { data: [ { id: null, value: 'someValue', data: 'someData' } ...

Moving from the center to the bottom-right with a CSS transition

I have a specific requirement where I need the container to cover the entire page and shrink when clicked, with an animation. Currently, I am using CSS transition to animate the container shrinking towards the top: The container shrinks slowly to the sp ...

"Learn the best method for accessing the currently logged-in user in Vue.js

After implementing JWT authentication in my ASP.NET Core and Vue.js application, I have created the following auth controller: [Route("Login")] [HttpPost] public IActionResult Login(LoginArgument loginArgument) { var user = _userServic ...

Combine the numerical values from the input field in the text area

Just starting out with JavaScript and running into an issue.. I'm trying to add the numbers inputted in a TEXTAREA, but for some reason I keep getting an array as the output. Can someone help me identify what's wrong and provide a solution? Thank ...

One-page application featuring preloaded data from a backend API

Imagine building a single-page application that relies heavily on client-side interactions communicating with the server through API methods. When we land on the index page that displays all records from the database, we essentially make two initial requ ...

Using AJAX to save and retrieve image data from cookies

Is there a way to download images only once when they are identical, saving network traffic in the process? Looking for a solution using AJAX Updates. Appreciate any help, Kuldeep Dwivedi ...

Effortless Page Navigation - Initial click may lead to incorrect scrolling position, but the subsequent link functions perfectly

While implementing the Smooth Page Scroll code from CSS Tricks, we encountered an issue where clicking on a navigation link for the first time only scrolls down to a point that is approximately 700px above the intended section. Subsequent clicks work perfe ...

Using regex to eliminate emojis, such as 🔜 and more

Previously, I attempted to remove hashtags and comments from Twitter. The string I worked with looked like this: @lien_ayy92 ...

My goal is to create a React js application that can automatically generate unique card designs

I've been working on developing cards using react js that are automatically generated based on API data. However, all the cards currently display one below the other and I'm aiming to have them designed in a row fashion. I've tried various m ...

Exploring ASP.NET Core 3.0: Animated Loading Icons

My latest project involves integrating a loading GIF, and I'm using ASP.NET Core 3.0 with Razor Pages for this task. It's been challenging for me as a newcomer to incorporate some Javascript or Ajax code to achieve the desired functionality. Upon ...

The script generated by document.write is failing to function as expected

A completed JQuery script has been created to enable the movement of a 360° object (picture) based on mouse actions. The code is functional: <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title> ...

Is it possible to combine asynchronous and synchronous functions in the same code?

I've recently started experimenting with Node.js and I'm running into issues with asynchronous functions. While I was able to create a small game, the only way I could successfully integrate asynchronous functions with synchronous functions was b ...

What's the best way to trigger an event within a tag in a Vue.js component?

app.js Vue.component("popup",{ template : /*html*/` <div class="popup is-active" > <div class="popup-background"></div> <div class="popup-card"> <header class=" ...

I can't figure out why my code isn't functioning properly. My goal is to have a PDF file generated when the submit button is clicked on a form, and for a

Hello everyone, I am new to the world of programming, but I have been assigned a project that involves using jQuery, JavaScript, html2pdf, and Bootstrap 5. I seem to be facing some issues with my code. Can someone please help me identify what's wron ...

Assistance required with Jquery logic for managing different divs with individual triggers. Ensure only one div is open at a time and close all others when a new one is opened

http://jsfiddle.net/nicktheandroid/ZSvHK/ I am facing some challenges with my current code and would appreciate any suggestions for improvement. Here are the issues I am encountering: I have a set of divs, each with its own trigger button. Only one menu ...

Shuffling specific table cells to exchange positions

I am attempting to create a script that will allow certain td elements (but not all) in different tr elements to switch places with the ones directly above or below them. My goal is to only target the last 3 td elements in each row, rather than the entire ...