jquery - Accessing properties of html element inside javascript function using $(this) gives undefined -
i calling function test() on anchor tag this:-
<a class="link" onclick="test();">hello</a>   and trying access class of anchor inside function test():-
function test() {   alert($(this).attr("class")); }   but alerts undefined. know calling onclick=test(this); instead , having following function works:-
function test(obj) {   alert($(obj).attr("class")); }   but, wanted access properties of element (on function called) without passing this. able get/set properties of element (on function called). possible? how this? 
in scenario need make sure html element owns function wanna call upon click. u doing u using onclick attribute takes string parameter , oblivious of else.
in order make html element owner of function need map onclick property completely own custom function. in javascript that
element.onclick = dosomething;   so our code
<div id="element">click</div> <script> function dosomething(){     this.style.color= "#000000" } document.getelementbyid("element").onclick = dosomething; </script>   here working example http://www.jsfiddle.net/vmcmb/1/.
also please take @ http://www.quirksmode.org/js/this.html.
Comments
Post a Comment