137

How to change the href attribute value of an <a/> tag through Javascript on button click ?

<script type="text/javascript">
  function f1()
  {
    document.getElementById("abc").href="xyz.php"; 
  }
</script>

<a href="" id="abc">jhg</a>
<a href="" id="" onclick="f1()">jhhghj</a>
Joseph Marikle
  • 68,672
  • 14
  • 103
  • 120
Chauhan
  • 2,021
  • 7
  • 31
  • 43

8 Answers8

195

Without having a href, the click will reload the current page, so you need something like this:

<a href="#" onclick="f1()">jhhghj</a>

Or prevent the scroll like this:

<a href="#" onclick="f1(); return false;">jhhghj</a>

Or return false in your f1 function and:

<a href="#" onclick="return f1();">jhhghj</a>

....or, the unobtrusive way:

<a href="#" id="abc">jhg</a>
<a href="#" id="myLink">jhhghj</a>

<script type="text/javascript">
  document.getElementById("myLink").onclick = function() {
    document.getElementById("abc").href="xyz.php"; 
    return false;
  };
</script>
Nick Craver
  • 594,859
  • 130
  • 1,270
  • 1,139
42

Exactly what Nick Carver did there but I think it would be best if used the DOM setAttribute method.

<script type="text/javascript">
   document.getElementById("myLink").onclick = function() {
   var link = document.getElementById("abc");
   link.setAttribute("href", "xyz.php");
   return false;
   }
</script>

It's one extra line of code but find it better structure-wise.

jakobhans
  • 696
  • 7
  • 15
  • 9
    IMO there's no need here, this has been a DOM property forever, `.href` works in all browsers...for example would you use `.getAttribute("id")` instead of just `.id`? :) – Nick Craver Dec 06 '10 at 10:34
  • 3
    You're right, it doesn't do anything special or different, it's just structural-wise. I like having all my DOM functions this way for later debugging/review: It's easier for me to see what the function is doing with this methods. Obviously your answer is right on the money :) – jakobhans Dec 06 '10 at 10:50
8

remove href attribute:

<a id="" onclick="f1()">jhhghj</a>

if link styles are important then:

<a href="javascript:void(f1())">jhhghj</a>
atlavis
  • 2,589
  • 1
  • 18
  • 18
2
<a href="#" id="a" onclick="ChangeHref()">1.Change 2.Go</a>

<script>
function ChangeHref(){
document.getElementById("a").setAttribute("onclick", "location.href='http://religiasatanista.ro'");
}
</script>
user3325593
  • 649
  • 5
  • 5
1

To have a link dynamically change on clicking it:

<input type="text" id="emailOfBookCustomer" style="direction:RTL;"></input>
        <a 
         onclick="this.href='<%= request.getContextPath() %>/Jahanpay/forwardTo.jsp?handle=<%= handle %>&Email=' + document.getElementById('emailOfBookCustomer').value;" href=''>
    A dynamic link 
            </a>
Xavi López
  • 26,413
  • 11
  • 94
  • 146
Mohsen Abasi
  • 1,179
  • 15
  • 25
1

Here's my take on it. I needed to create a URL by collecting the value from a text box , when the user presses a Submit button.

<html>
<body>

Hi everyone

<p id="result"></p>

<textarea cols="40" id="SearchText" rows="2"></textarea>

<button onclick="myFunction()" type="button">Submit!</button>

<script>
function myFunction() {
    var result = document.getElementById("SearchText").value;
 document.getElementById("result").innerHTML = result;
 document.getElementById("abc").href="http://arindam31.pythonanywhere.com/hello/" + result;
}  
</script>


<a href="#" id="abc">abc</a>

</body>
<html>
Arindam Roychowdhury
  • 3,975
  • 5
  • 43
  • 52
0
<script type="text/javascript">
  function f1(mHref)
  {
    document.getElementById("abc").href=mHref; 
  }
</script>

<a href="" id="abc">jhg</a>
<button onclick="f1("dynamicHref")">Change HREF</button>

Just give the dynamic HREF in Paramters
-1

I know its bit old post. Still, it might help some one.

Instead of tag,if possible you can this as well.

 <script type="text/javascript">
        function IsItWorking() {
          // Do your stuff here ...
            alert("YES, It Works...!!!");
        }
    </script>   

    `<asp:HyperLinkID="Link1"NavigateUrl="javascript:IsItWorking();"`            `runat="server">IsItWorking?</asp:HyperLink>`

Any comments on this?

4u.Ans
  • 1,229
  • 14
  • 22