SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Guru
    Join Date
    Feb 2006
    Location
    Chepstow, South Wales
    Posts
    907
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to use javascript in href to call function and use anchor text

    What I have is a situation where I am using java script to open and close div's but those divs are down the page with some of the links I'm using.

    So what I want to do is use achor text, but I dont know if I can.

    below is the code.

    Code:
    <div style="border: 1px solid #ffffff; background-color: #221816; padding: 6px; position:relative; float:left; left:-3px; font-size:14px;">
    <a id="myHeader1-2" href="javascript:showonlyonev2('newboxes1-2');" >Medieval Banquets</a>
    </div>
    What I would like to do is add #info to pick up an anchor further down the page, so something liek this:

    Code:
    <div style="border: 1px solid #ffffff; background-color: #221816; padding: 6px; position:relative; float:left; left:-3px; font-size:14px;">
    <a id="myHeader1-2" href="javascript:showonlyonev2('newboxes1-2');#info" >Medieval Banquets</a>
    </div>
    Can this be done?

  2. #2
    SitePoint Guru
    Join Date
    Feb 2006
    Location
    Chepstow, South Wales
    Posts
    907
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK I worked it out.

    Rather than trying to attach the #info in the link part, I moved it to the end of the script itself.

    Code:
    <script language="javascript">
    function showonlyonev2(thechosenone) {
          var newboxes = document.getElementsByTagName("div");
          for(var x=0; x<newboxes.length; x++) {
                name = newboxes[x].getAttribute("name");
                if (name == 'newboxes-2') {
                      if (newboxes[x].id == thechosenone) {
                            if (newboxes[x].style.display == 'block') {
                                  newboxes[x].style.display = 'none';
                            }
                            else {
                                  newboxes[x].style.display = 'block';
                            }
                      }else {
                            newboxes[x].style.display = 'none';
                      }
                }
          }
    window.location = "http://www.mywebsite.co.uk/index.php#info"
    } 
    </script>
    Cheers for those who looked


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •