1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Java Script help

Discussion in 'JavaScript' started by Paulley, Apr 4, 2008.

  1. #1
    Hi guys, im a super-noob at this website stuff and i really need a little help.

    I have been using some script to toggle show/hide text but it starts off with the text being seen and you have to click it to hide

    
    <script type="text/javascript">
    function toggleDisplay(mySpan) {
      // acquire a reference to the target div by looking for first sibling div
      var target = mySpan.parentNode.parentNode.getElementsByTagName("DIV")[0];
      // hide or show div, and swap the text in the span
      if (target.style.display!="none") {
        // hide the target div
        target.style.display = "none";
        mySpan.innerHTML = "<U>REVEAL THE ABOVE</U>";
      } else {
        // reveal the target div
        target.style.display = "block";
        mySpan.innerHTML = "<U>HIDE THE ABOVE</U>";
      }
    }
    </script> 
    
    <div class="showHideContainer">
      <div style="border: 1px solid black">
    blah blah blah
    </div>
      <script type="text/javascript">
      document.write("<p style=\"margin-top:0; text-align:center\"><span " +
        "onclick=\"toggleDisplay(this);\" style=\"cursor:pointer\">Hide " +
        "the above</span></p>\n");
      </script>
    </div>
    
    Code (markup):
    But to help unclutter my page, i wanted to know how to change it so that the text starts off hidden, having to be clicked to reveal.

    any help would be really appreciated :)
     
    Paulley, Apr 4, 2008 IP
  2. MMJ

    MMJ Guest

    Messages:
    460
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #2
    <script type="text/javascript">
    function toggleDisplay(mySpan) {
      // acquire a reference to the target div by looking for first sibling div
      var target = mySpan.parentNode.parentNode.getElementsByTagName("DIV")[0];
      // hide or show div, and swap the text in the span
      //alert(target.style.display);
      if (target.style.display == "none") {
        // reveal the target div
        target.style.display = "block";
        mySpan.innerHTML = "<U>HIDE THE ABOVE</U>";
      } else {
        // hide the target div
        target.style.display = "none";
        mySpan.innerHTML = "<U>REVEAL THE ABOVE</U>";
      }
    }
    </script>
    
    <div class="showHideContainer">
      <div style="border: 1px solid black; display:none">
    blah blah blah
    </div>
      <p style="margin-top:0; text-align:center">
        <span onclick="toggleDisplay(this);" style="cursor:pointer">Reveal the above</span></p>
    </div>
    HTML:
     
    MMJ, Apr 4, 2008 IP
  3. Paulley

    Paulley Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    thank you +1
     
    Paulley, Apr 11, 2008 IP