173

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
  • 76,418
  • 17
  • 112
  • 129
Chauhan
  • 2,441
  • 8
  • 37
  • 46

8 Answers8

230

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
  • 623,446
  • 136
  • 1,297
  • 1,155
54

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.

Harshil Modi
  • 397
  • 3
  • 8
  • 15
jakobhans
  • 826
  • 7
  • 16
  • 11
    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
  • 4
    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
  • Thank you! still works today! – mick1996 Jan 18 '22 at 15:16
7

remove href attribute:

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

if link styles are important then:

<a href="javascript:void(f1())">jhhghj</a>
RobertO
  • 2,655
  • 1
  • 20
  • 18
6

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
  • 5,927
  • 5
  • 55
  • 63
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
  • 809
  • 6
  • 5
2
<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
Abhishek K
  • 41
  • 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
  • 27,550
  • 11
  • 97
  • 161
Mohsen Abasi
  • 2,050
  • 28
  • 30
-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,279
  • 14
  • 22