views:

127

answers:

2

This is my JavaScript function:

function switch_div(firstID,secondID) {

    if((document.getElementById(firstID).style.display  == 'inline') &&
       (document.getElementById(secondID).style.display == 'inline')) {
       document.getElementById(firstID).style.display = 'none';

    } else if(document.getElementById(firstID).style.display == 'inline'){
       document.getElementById(firstID).style.display  = 'none';
       document.getElementById(secondID).style.display = 'inline';

    } else if (document.getElementById(firstID).style.display == 'none') {

       document.getElementById(firstID).style.display  = 'inline';
       document.getElementById(secondID).style.display = 'none';
    }
}

And this is my link and the two s I want to switch between them by clicking the same link each time:

<a href="#" onclick="switch_div('first_choice','second_choice');return false" style="position:relative">

<div id="first_choice" style="position:relative;">hello</div>

<div id="second_choice" style="position:relative;">bye</div>

This link to the JavaScript function works fine in Opera but not at all in Firefox. Why is that the case?

+2  A: 

Provided style attributes of display to both the divs. Better to give them in classes. Hope this is what you were looking for.

<a href="#" onclick="switch_div('first_choice','second_choice');return false" style="position:relative">
<div id="first_choice" style="position:relative;display: inline">hello</div>
<div id="second_choice" style="position:relative;display: none">bye</div>

<script>

function switch_div(firstID,secondID) 
{ 

        if((document.getElementById(firstID).style.display == 'inline') && (document.getElementById(secondID).style.display == 'inline'))
    {

     document.getElementById(firstID).style.display = 'none';
    }

        else if(document.getElementById(firstID).style.display == 'inline')
    {

            document.getElementById(firstID).style.display = 'none';

            document.getElementById(secondID).style.display = 'inline';
    }

        else if (document.getElementById(firstID).style.display == 'none') 
    {

          document.getElementById(firstID).style.display = 'inline';

          document.getElementById(secondID).style.display = 'none';
    }
}

</script>

Working demo: http://jsbin.com/iqeli/

rahul
yeah,thanks,this is what i had set in onload event and forgot to write here,thank you very much
adi
+2  A: 

I see no cross-browser issue: for me, it doesn't work in Opera either. That's not too surprising: you're checking the ‘style’ attribute for inline display, but neither of the divs have ‘display: inline’ there.

Either way, inline is not the usual ‘display’ model for div, you'd usually have ‘block’.

Suggest:

<style type="text/css">
    .hidden { display: none; }
</style>

<a href="#first_choice" id="togglelink">Clickety</a>
<div id="first_choice">
    blah 1
</div>
<div id="second_choice">
    blah 2
</div>

<script type="text/javascript">
    function hideToggler(link, a, b) {
        var state= link.hash.substring(1)==a.id; // set which div is shown initially from the link destination
        link.onclick= function() { // toggle divs
            state= !state;
            a.className= state? 'hidden' : '';
            b.className= state? '' : 'hidden';
            return false;
        };
        link.onclick(); // hide one of the divs to begin with
    }

    hideToggler(
        document.getElementById('togglelink'),
        document.getElementById('first_choice'),
        document.getElementById('second_choice')
    );
</script>
bobince
hi,thank you very much for replying.i tried your code but it also doesn't work for me,i wonder why?the link appears but as i click on it nothing happens!then i tried your code sepretaly but strange!it doesn't work here :(...wondering why?
adi
oh my fault...it's working now,thank you!
adi