I am trying to make a jquery function, and used below code, but not getting any output. Please tell me the process to make it.
<script type="text/javascript">
(function($){
$.fn.myplugin = function(){
alert(this.id);
};
})(jQuery);
$("#sample").myplugin();
</script>
You can see the working code here – http://jsfiddle.net/gryzzly/YxnEQ/
Note that you need an element with id “sample” to be present in your document when you are running the above code. Also, the
thisis pointing at jQuery object, so in order to get the elements id, you need to use either of the following:or
The first is preferable, because it’s faster.