JSFiddle - React, Tailwind, and code Playground
HTML
<div id=sample>This is a script that would be useful to run within other functions</div>
JavaScript
$( document ).ready(function() {
// Also note, that for $('#sample').myplugin() to work
// you need the element with id "sample" to be existent
// so the function is actually called
(function($){
$.fn.myplugin = function(){
// in your code, the `this` is pointing to
// jQuery object, you can't access `id` attribute
// with dot. You need to either access first element
// in jQuery object (`this[ 0 ]`) or use this.attr('id')
// alert( this[0].id );
alert( "This element has an id of #'"+this[0].id+"'! After closing this, click the paragraph to bring this alert back." );
};
})(jQuery);
$( "#sample" ).click(function() {
$( this ).myplugin();
});
$("#sample").myplugin();
});