access element from javascript function to jquery function -


var id=0;// globel   var elem=0;// globel  $.fn.myfunction = function(element){      var sid = element.getattribute('href');       elem=element;         id=sid;  };  $(document).ready(function(){    	$.fn.myfunction();  	alert(id);  	alert(elem);  });
<a href="#div-1" id="link-id-1" onclick="$.fn.myfunction(this);">link 1</a><br>  <a href="#div-2" id="link-id-2" onclick="$.fn.myfunction(this);">link 2</a><br>  <div id="div-1" class="hideall">aaaaaaaaaaaaaaaaa</div><br>  <div id="div-2" class="hideall">bbbbbbbbbbbbbbbbbb</div>

this code not working want alert id attributes "link-id-1" or "link-id-2" , href attributes "#id-1" or "#id-2" javascript function (myfunction()) jquery function

there basic error in code. calling $.fn.myfunction on $(document).ready. not have value element.

use -

var id=0;// globel  var elem=0;// globel $.fn.myfunction = function(element){ var sid = element.href;  elem=element;    id=sid; alert(id); alert(elem); }; 

Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -