html - JavaScript get href onclick -
i trying return href
attribute of link using javascript when user clicks on it. want url link linking displayed in alert instead of loading page.
i have following code far:
function doalert(){ alert(document.getelementbyid("link").getattribute("href")); return false; }
with following markup:
<a href="http://www.example.com/" id="link" onclick="return doalert()">link</a>
for reason, no alert ever displayed , page loads instead. know why is?
using jquery not option in case.
seems order of code, try this
<script> function doalert(obj) { alert(obj.getattribute("href")); return false; } </script> <a href="http://www.example.com/" id="link" onclick="doalert(this); return false;">link</a>
Comments
Post a Comment