Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

function in HREF not working using javascript

Tags:

javascript

I have function which needs to be run on click event. However it is working fine with onclick attribute of anchor tag but not working with HREF attribute. It is giving id undefined. Here is demo code

HTML

<a href="javascript:test(this)" id="first">click</a>

JS

function test(obj){
alert(obj.id)
}
like image 750
Jitender Avatar asked Sep 20 '13 18:09

Jitender


1 Answers

this in that context is the window, not the control. Placing javascript in the href is not the same as an event handler like onclick. Here's a fiddle.

like image 189
canon Avatar answered Sep 27 '22 19:09

canon