I wanted to create jquery plugin & started off creating a sample jquery plugin…But
$(this).css('color','blue');
alert(this.id);
doesn’t work…
jquery.myPlugin.js
jQuery.fn.changeTextColor = function() {
alert("xx"); //this works
return this.each(function() {
$(this).css('color','blue');
alert(this.id); //both the 2 statements don't work..
});
};
HTML
<script type = "text/javascript" src="jquery-1.7.1.js"></script>
<script type = "text/javascript" src="jquery.myPlugin.js"></script>
<script>
$('#mypara').changeTextColor();
</script>
</head>
<body>
<p id="mypara">dsfdsfdsf</p>
</body>
Thar she blows!
Then call with:
Have a fiddle 😉
http://jsfiddle.net/nmkeD/