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> 

http://jsfiddle.net/yz8yv/

http://jsfiddle.net/yz8yv/2/


Comments

Popular posts from this blog

javascript - Laravel datatable invalid JSON response -

java - Exception in thread "main" org.springframework.context.ApplicationContextException: Unable to start embedded container; -

sql server 2008 - My Sql Code Get An Error Of Msg 245, Level 16, State 1, Line 1 Conversion failed when converting the varchar value '8:45 AM' to data type int -