javascript - Getting value from this JQuery plugin -


i've been experimenting writing jquery plugins lately, , i'm sure simple question. but, seem not able value inside of plugin.

for example, have following code:

plugin.js

$(function($) {   $.fn.myplugin = function() {     // alert id main.js   } }); 

main.js

$(document).ready(function() {   $('#somediv').attr('id').myplugin(); }); 

you cannot define plugin on string. use selector call plugin.

have @ this.

use this:

(function ($) {     $.fn.myplugin = function () {         this.each(function () {             // allow multiple element selectors             alert(this.attr('id'));         });          return this; // allow chaining     } } (jquery));  $('.myclass').myplugin(); 

demo

tutorial


Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -