Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

code for firefox

Status
Not open for further replies.

newbee1981

Programmer
Feb 21, 2008
8
US
Hi I have the following code that works for IE but does not for firefox or netscape. Can anyone help me convert this for firefox compatible thanks.

<script type="text/javascript">
var checkBoxes = new Array;
function setup()
{
debugger;
var oInputs = document.getElementsByTagName('input');
for ( i = 0; i < oInputs.length; i++ )
{
// loop through and find <input type="checkbox"/>
if ( oInputs.type == 'checkbox')
{
checkBoxes.push( oInputs ); // found one - store it in the oTextBoxes array
oInputs.attachEvent("onclick", checkboxClicked);
}
}
}

function checkboxClicked()
{
if (window.event.srcElement.checked)
{
// Was a mex clicked?
if (window.event.srcElement.nextSibling.innerHTML.indexOf("MEX") > 0)
{
for ( i = 0; i < checkBoxes.length; i++ )
{
// uncheck everything except the one the user selected.
if (checkBoxes != window.event.srcElement && checkBoxes.nextSibling.innerHTML.indexOf("MEX") < 0)
checkBoxes.checked = false;
}
}
else
{
// A mex one was not selected so uncheck any mex ones that are checked
for ( i = 0; i < checkBoxes.length; i++ )
{
// uncheck everything except the one the user selected.
if (checkBoxes != window.event.srcElement && checkBoxes.nextSibling.innerHTML.indexOf("MEX") > 0)
checkBoxes.checked = false;
}
}
}
}

</script>
 
The likely problem is that Firefox treats whitespace as a dom node, while IE does not. Read up on either of these 2 pages to get a full explanation and solutions:


-kaht

Lisa, if you don't like your job you don't strike. You just go in every day and do it really half-assed. That's the American way. - Homer Simpson

[small]<P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <.</B>[/small]
 
Hi newbee,
event.srcElement isn't standard. Use event.target instead.
Dave.
 
Good catch starsky51, I overlooked that :)

-kaht

Lisa, if you don't like your job you don't strike. You just go in every day and do it really half-assed. That's the American way. - Homer Simpson

[small]<P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <.</B>[/small]
 
thanks for your reply.
But could you help me modifying my existing code to work for firefox.
 
I made some modification .
Here is the code:

<script type="text/javascript">
var checkBoxes = new Array;
function setup(e)
{
debugger;
var oInputs = document.getElementsByTagName('input');
for ( i = 0; i < oInputs.length; i++ )
{
// loop through and find <input type="checkbox"/>
if ( oInputs.type == 'checkbox')
{
checkBoxes.push( oInputs ); // found one - store it in the oTextBoxes array
oInputs.addEventListener("click", checkboxClicked(e), false);
}
}
}

function checkboxClicked(e)
{
debugger;
var node = e.target;

if (event.target.checked)
{
// Was a mex clicked?
if (event.target.nextSibling.innerHTML.indexOf("MEX") > 0)
{
for ( i = 0; i < checkBoxes.length; i++ )
{
// uncheck everything except the one the user selected.
if (checkBoxes != event.target && checkBoxes.nextSibling.innerHTML.indexOf("MEX") < 0)
checkBoxes.checked = false;
}
}
else
{
// A mex one was not selected so uncheck any mex ones that are checked
for ( i = 0; i < checkBoxes.length; i++ )
{
// uncheck everything except the one the user selected.
if (checkBoxes != event.target && checkBoxes.nextSibling.innerHTML.indexOf("MEX") > 0)
checkBoxes.checked = false;
}
}
}
}

</script>
I am calling it at body onLoad as setup(event);
 
newbee,
Code:
  <script type="text/javascript">
   var checkBoxes = new Array;
   function setup(e)
    {
        debugger;
        var oInputs = document.getElementsByTagName('input');
        for ( i = 0; i < oInputs.length; i++ )
        {
            // loop through and find <input type="checkbox"/>
            if ( oInputs[i].type == 'checkbox')
            {
                checkBoxes.push( oInputs[i] ); // found one - store it in the oTextBoxes array
                 oInputs[i].addEventListener("click", [COLOR=red]checkboxClicked[/color], false);
            }
        }     
    }
    
    function checkboxClicked(e)
    {
        debugger;
        var node = e.target;
           
        if ([COLOR=red]node[/color].checked)
        {
            // Was a mex clicked?
            if ([COLOR=red]node[/color].nextSibling.innerHTML.indexOf("MEX") > 0)
            {
                for ( i = 0; i < checkBoxes.length; i++ )
                {
                    // uncheck everything except the one the user selected.
                    if (checkBoxes[i] != [COLOR=red]node[/color] && checkBoxes[i].nextSibling.innerHTML.indexOf("MEX") < 0)
                        checkBoxes[i].checked = false;
                }
            }
            else
            {
                // A mex one was not selected so uncheck any mex ones that are checked
                for ( i = 0; i < checkBoxes.length; i++ )
                {
                    // uncheck everything except the one the user selected.
                    if (checkBoxes[i] != [COLOR=red]node[/color] && checkBoxes[i].nextSibling.innerHTML.indexOf("MEX") > 0)
                        checkBoxes[i].checked = false;
                }
            }        
        }                
    }
    
    </script>
 
The code that you've already written is using advanced javascript dom methods - more than what I would expect a novice javascripter to know. If you were the one that wrote that function originally, you should have no problem reading the links I posted and making the appropriate changes.

starsky51 and I have shown you what is wrong. Now that you know what's broken, don't you think you'd learn more if you fix it yourself?


Read here for a better description.

-kaht

Lisa, if you don't like your job you don't strike. You just go in every day and do it really half-assed. That's the American way. - Homer Simpson

[small]<P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <.</B>[/small]
 
sorry I did not write that code. I am not very well versed with javascript thats why I was not able to understand that post. I am modifying some site that was written by some other coder but was IE specific only. So thats why I needed some help.
Thanks all the above code did solve my purpose.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top