Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

What's wrong with this javascript?

  • 29-11-2005 10:55AM
    #1
    Registered Users, Registered Users 2 Posts: 1,384 ✭✭✭


    I'm trying to show and hide a layer based on a click. The previous script didn't work in Firefox, this new version doesn't work in any browser! What's wrong with it?!


    function toggleVisibility(me) {
    var object = document.getElementById(me);
    state = object.style.visibility;
    if (state == "hidden"){
    object.style.visibility ="visible";
    image_text.innerText="Hide Images";
    }
    else {
    object.style.visibility ="hidden";
    image_text.innerText="Show Images";
    }
    }



    Thanks.


Comments

  • Registered Users, Registered Users 2 Posts: 21,278 ✭✭✭✭Eoin


    How are you invoking the JScript function? works fine for me in IE and Firefox using the code like this:

    [HTML]<html>
    <head>
    <title>test</title>
    </head>
    <script type="text/JavaScript">
    function toggleVisibility(me)
    {
    var object = document.getElementById(me);
    state = object.style.visibility;
    if (state == "hidden")
    {
    object.style.visibility ="visible";
    image_text.innerText="Hide Images";
    }
    else
    {
    object.style.visibility ="hidden";
    image_text.innerText="Show Images";
    }
    }
    </script>
    <body>
    <div id="theDiv">Some Text</div>
    <input type="button" onClick="toggleVisibility('theDiv')" value="Hide Images" id="image_text">
    </body>
    </html>[/HTML]


  • Registered Users, Registered Users 2 Posts: 1,384 ✭✭✭chabsey


    eoin_s wrote:
    How are you invoking the JScript function? works fine for me in IE and Firefox using the code like this:

    This is the invocation:

    <a href="javascript:toggleVisibility('CarPhoto')"><span id="image_text" >Hide
    Images</span></a>

    Still doesn't work. I presume it's something to do with me not using a button?


  • Registered Users, Registered Users 2 Posts: 21,278 ✭✭✭✭Eoin


    chabsey wrote:
    This is the invocation:

    <a href="javascript:toggleVisibility('CarPhoto')"><span id="image_text" >Hide
    Images</span></a>

    Still doesn't work. I presume it's something to do with me not using a button?

    I don't think it's to do with a button. You are sending "CarPhoto" in as the object to get the style of, but where is that object specified?


  • Registered Users, Registered Users 2 Posts: 1,384 ✭✭✭chabsey


    eoin_s wrote:
    I don't think it's to do with a button. You are sending "CarPhoto" in as the object to get the style of, but where is that object specified?


    Just above the togglevisibility, this div is set :

    <div id="CarPhoto" >
    PHOTO HERE
    </div>


    And in the CSS it is declared as:


    #CarPhoto {
    float:right;
    margin-left: 5px;
    padding: 2px;
    background: #F0F0F0;
    }


  • Registered Users, Registered Users 2 Posts: 21,278 ✭✭✭✭Eoin


    Try this - I changed it so instead of using an ID in the css, I used two classnames instead

    [HTML]<html>
    <head>
    <title>test</title>
    </head>
    <script type="text/JavaScript">
    function toggleVisibility(me)
    {
    var sClass = document.getElementById(me).className;

    if (sClass == "ShowCarPhoto")
    {
    document.getElementById(me).className = "HideCarPhoto";
    document.getElementById("image_text").innerHTML ="Show Images";
    }
    else
    {
    document.getElementById(me).className = "ShowCarPhoto";
    document.getElementById("image_text").innerHTML ="Hide Images";
    }
    }
    </script>
    <style type="text/css">
    .ShowCarPhoto
    {
    float:right;
    margin-left: 5px;
    padding: 2px;
    background: #F0F0F0;
    visibility: visible;
    }
    .HideCarPhoto
    {
    visibility: hidden;
    }
    </style>
    <body>
    <a href="javaScript: toggleVisibility('CarPhoto')"><div id="image_text">Hide Images</div></a>

    <div class="ShowCarPhoto" id="CarPhoto">
    PHOTO HERE
    </div>
    </body>
    </html>[/HTML]


  • Advertisement
  • Moderators, Science, Health & Environment Moderators Posts: 9,205 Mod ✭✭✭✭mewso


    The problem may be with the javascript call in the href. You could add ";return false" to the end or change it to href="#" onclick="javascript..."


  • Registered Users, Registered Users 2 Posts: 21,278 ✭✭✭✭Eoin


    OP - how did you get on?


  • Registered Users, Registered Users 2 Posts: 1,384 ✭✭✭chabsey


    eoin_s wrote:
    OP - how did you get on?

    Still didn't work for me. I've ended up using a version that works in IE but not in Firefox. Baffles me what I was doing wrong.


  • Registered Users, Registered Users 2 Posts: 21,278 ✭✭✭✭Eoin


    chabsey wrote:
    Still didn't work for me. I've ended up using a version that works in IE but not in Firefox. Baffles me what I was doing wrong.

    unless you post your HTML, not much we can help you with - I could only include the snippets you posted.


Advertisement