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>
Exactly what Nick Carver did there but I think it would be best if used the DOM setAttribute method.
It's one extra line of code but find it better structure-wise.
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.
To have a link dynamically change on clicking it:
remove
href
attribute:if link styles are important then:
Without having a
href
, the click will reload the current page, so you need something like this:Or prevent the scroll like this:
Or
return false
in yourf1
function and:....or, the unobtrusive way: