Making a jQuery plugin

Making a jQuery plugin

In this tutorial, we will see how to make a jquery plugin. First step is to create a self executing function. Inside this function we will create our own functions e.g. if we want to create a function then we should pass the jquery object to the self executing function to define a function just add property of the $.fn object e.g if the function name is test the definition should be $.fn.test = function(<optional_parameters>) {}; To call the function, just use $("selector").test();

In this tutorial, we will see how to make a chess borad (8 * 8),
First step is to print the board and then place the correct animals in the correct positions.
1.$.fn.board - In this method, we make the design of the board using javascript ECMA script 6
2.$.fn.initilaize - In this method, we place the animals in their appropriate positions


<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
(function($){
	$.fn.board = function(e) {
	for(let t=0;e*e>t++;)$(".container").append($("<div />"));for(let o=$(".container  div"),l=0,r=50,a=50,d=0,n=["rgb(255,225,0)","rgb(255,150,0)"],s=0;e>s++;){1&e?++d:d&1,l=0;for(let i=-9;e>i++;)$(o[e*s+i]).css({"background-color":n[d],"left":l,"top":r,"width":a,"height":a,"position":"absolute"}),l+=a,d=d+1&1;r+=a}
	};
	
	$.fn.initialize = function() {
	
	$(".container div").each(function(index) {
	$(this).css({paddingTop:"15px",textAlign:"center"});
		if(index == 0)$(this).addClass("r1_c1");
		if(index == 1)$(this).addClass("r1_c2");
		if(index == 2)$(this).addClass("r1_c3");
		if(index == 3)$(this).addClass("r1_c4");
		if(index == 4)$(this).addClass("r1_c5");
		if(index == 5)$(this).addClass("r1_c6");
		if(index == 6)$(this).addClass("r1_c7");
		if(index == 7)$(this).addClass("r1_c8");
		if(index == 8)$(this).addClass("r2_c1");
		if(index == 9)$(this).addClass("r2_c2");
		if(index == 10)$(this).addClass("r2_c3");
		if(index == 11)$(this).addClass("r2_c4");
		if(index == 12)$(this).addClass("r2_c5");
		if(index == 13)$(this).addClass("r2_c6");
        if(index == 14)$(this).addClass("r2_c7");
		if(index == 15)$(this).addClass("r2_c8");
		if(index == 48)$(this).addClass("r7_c1");
		if(index == 49)$(this).addClass("r7_c2");
		if(index == 50)$(this).addClass("r7_c3");
		if(index == 51)$(this).addClass("r7_c4");
		if(index == 52)$(this).addClass("r7_c5");
		if(index == 53)$(this).addClass("r7_c6");
		if(index == 54)$(this).addClass("r7_c7");
		if(index == 55)$(this).addClass("r7_c8");
		if(index == 56)$(this).addClass("r8_c1");
		if(index == 57)$(this).addClass("r8_c2");
		if(index == 58)$(this).addClass("r8_c3");
		if(index == 59)$(this).addClass("r8_c4");
		if(index == 60)$(this).addClass("r8_c5");
		if(index == 61)$(this).addClass("r8_c6");
		if(index == 62)$(this).addClass("r8_c7");
		if(index == 63)$(this).addClass("r8_c8");
		});
		$(".r1_c1,.r1_c8,.r8_c1,.r8_c8").text("E");$(".r1_c2,.r1_c7,.r8_c2,.r8_c7").text("H");$(".r1_c3,.r1_c6,.r8_c3,.r8_c6").text("C");
		$(".r1_c4,.r8_c4").text("K");$(".r1_c5,.r8_c5").text("Q");
		$(".r2_c1,.r2_c2,.r2_c3,.r2_c4,.r2_c5,.r2_c6,.r2_c7,.r2_c8,.r7_c1,.r7_c2,.r7_c3,.r7_c4,.r7_c5,.r7_c6,.r7_c7,.r7_c8").text("P");
		}
	}(jQuery));
</script>
</head>
<body onload="$('body').board(8);$('body').initialize();">
<div class="container">

</div>
</body>
</html>

Sample output:

Comments

Popular posts from this blog

XPath for HTML markup

Apache Hadoop | Running MapReduce Jobs

Laravel | PHP | Basics | Part 2