How to change href of <a> tag on button click through javascript

JavascriptHrefAttr

Javascript Problem Overview


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>

Javascript Solutions


Solution 1 - Javascript

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>

Solution 2 - Javascript

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.

Solution 3 - Javascript

remove href attribute:

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

if link styles are important then:

<a href="javascript:void(f1())">jhhghj</a>

Solution 4 - Javascript

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>

Solution 5 - Javascript

<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>

Solution 6 - Javascript

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>

Solution 7 - Javascript

<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

Solution 8 - Javascript

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionChauhanView Question on Stackoverflow
Solution 1 - JavascriptNick CraverView Answer on Stackoverflow
Solution 2 - JavascriptjakobhansView Answer on Stackoverflow
Solution 3 - JavascriptatlavisView Answer on Stackoverflow
Solution 4 - JavascriptArindam RoychowdhuryView Answer on Stackoverflow
Solution 5 - Javascriptuser3325593View Answer on Stackoverflow
Solution 6 - JavascriptMohsen AbasiView Answer on Stackoverflow
Solution 7 - JavascriptAbhishek KView Answer on Stackoverflow
Solution 8 - Javascript4u.AnsView Answer on Stackoverflow