1

I can't figure out why this isn't working. It changes to the first link and wont keep going. I'd also need it to loop through so when you click it always circles through the links I have a php variable that is the last #panelNum.

<html>
<head>
<script language="JavaScript"><!--
function findLinkByHref(href) {
  for (var i=0; i<document.links.length; i++) {
    if (document.links[i].href == href) return i;
  }
  return -1;
}

function changeLinkHref(id,newHref,oldHref) {
  if (document.links.length > 0) {
    if (document.getElementById) {
      document.getElementById(id).href = newHref;
    }
    else if (document.all) {
      document.all[id].href = newHref;
    }
    else {
      var index = findLinkByHref(oldHref);
      if (index > -1)
        document.links[index].href = newHref;
    }
  }
}
//--></script>
</head>
<body>
<a id="myLink" href="#panel3" value="Change href" onClick="changeLinkHref('myLink','#panel4','#panel5','#panel5','#panel6')">somewhere</a>
</body>
</html>

2 Answers 2

1
function findLinkByHref(href) {
  for (var i=0; i<document.links.length; i++) {
    if (document.links[i].href == href) return i;
  }
}

if you return in an if, it still returns - thus stopping execution of your function.

This is the only for loop, thus the only place you are iterating through all of your links. It sounds like you want to do this in the changeLinkHref function, something like..

 else if (document.all) {
      document.all[id].href = newHref;
    }
    else {
      var index = findLinkByHref(oldHref);
      for (var i=0; i<document.links.length; i++) {
         // change all links?
Sign up to request clarification or add additional context in comments.

Comments

0

You can do something like this.

JS

function A()    {
document.getElementById("B").href="http://empireminecraft.com/";
}

HTML

<button type="button" onClick="A()">Yo!</button>
<a href="https://duckduckgo.com/?q=web+validator" id="B">Yo!</a>

I just used URLs that I had open. For some reason, when I tried it, I could only get it to work in IE

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.