Developer
Developer

Reputation: 8636

Why my javascript is not working in Firefox

I have written the following script to change the back color of check box when checked but this is not working on Mozila

<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

Upvotes: 1

Views: 687

Answers (1)

immutabl
immutabl

Reputation: 6903

HINT: Look at the JavaScript error console (CTRL + SHIFT + j), parentElement is not supported in Mozilla browsers.

Try using parentNode instead:

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

            if (inputItem.checked) {
                //inputItem.parentElement.style.backgroundColor = 'Red';//Won't work in Mozilla
                inputItem.parentNode.style.backgroundColor = 'Red';
            }
            else {
                //inputItem.parentElement.style.backgroundColor = 'White';//Won't work in Mozilla
                inputItem.parentNode.style.backgroundColor = 'White';

            }

Upvotes: 2

Related Questions