My Recipes In jQuery - DOM Manipulation Part 1

DOM stands for Document Object Model and is a mechanism for representing and interacting with your HTML, XHTML or XML documents. JQuery provides methods to manipulate DOM in an efficient way. You do not need to write lengthy code to modify the value of any element's attribute. One very important part of jQuery is the possibility to manipulate the DOM.

JQuery Document Object Model
                                                Figure:
JQuery Document Object Model

A few of these methods in this section manipulate the DOM in some manner.

  1. .attr ()
  2. .css ()
  3. .add Class ()
  4. .remove Class ()
  5. .val ()
  6. .attr ()

The attr () method sets or returns attributes and values of the selected elements. Get the value for each element individually using a looping construct such as jQuery's .each () or .map () method.

Syntax

Selector. attr (key, function)

attr

.css ()

JQuery includes a handy way to get and set CSS properties of elements. It’s really easy to change CSS with jQuery, this is the format of the .CSS () function.

Syntax

Selector.css ("property name")

css

.add Class ()

The add Class () method adds one or more class names to the selected elements. This method does not replace a class. It simply adds the class, appending it to any which may already be assigned to the elements.

Syntax

$(selector).add Class (class name, function (index, old class))

add Class

.remove Class ()

The removeClass () method removes one or more class names from the selected elements. If no class names are specified in the parameter, all classes will be removed.

Syntax

$(selector).removeClass(classname,function(index, current class))

remove Class

.val ()

The Val () method returns or sets the value attribute of the selected elements such as input, select and text area. The Val () method is mostly used with HTML form elements.

Syntax

$(selector).val()

selector

The following is the HTML code I used for this tutorial.

HTML

  1. <!DOCTYPE HTML>  
  2. <HTML>  
  3.   
  4. <HEAD>  
  5.     <TITLE>SAMPLE</TITLE>  
  6. </head>  
  7.   
  8. <body>  
  9.     <h1 align="center">  
  10. <imgclassimgclass="banner"src="https://cdn2.iconfinder.com/data/icons/micon-social-pack/512/jquery-128.png">  
  11. </h1>  
  12.     <form>  
  13.         <div id="shuffle">  
  14.             <div id="cell1">1</div>  
  15.             <div id="cell2">2</div>  
  16.             <div id="cell3">3</div>  
  17.             <div id="cell4">4</div>  
  18.             <div id="cell5">5</div>  
  19.             <div id="cell6">6</div>  
  20.             <div id="cell7">7</div>  
  21.             <div id="cell8">8</div>  
  22.             <div id="cell9">9</div>  
  23.             <div id="cell10">10</div>  
  24.             <div id="cell11">11</div>  
  25.             <div id="cell12">12</div>  
  26.             <div id="cell13">13</div>  
  27.             <div id="cell14">14</div>  
  28.             <div id="cell15">15</div>  
  29.             <div id="cell16">16</div>  
  30.             <div id="cell17">17</div>  
  31.             <div id="cell18">18</div>  
  32.             <div id="cell19">19</div>  
  33.             <div id="cell20">20</div>  
  34.             <div id="cell21">21</div>  
  35.             <div id="cell22">22</div>  
  36.             <div id="cell23">23</div>  
  37.             <div id="cell24">24</div>  
  38.             <div id="cell25">25</div>  
  39.             <div id="cell26">26</div>  
  40.             <div id="cell27">27</div>  
  41.             <div id="cell28">28</div>  
  42.             <div id="cell29">29</div>  
  43.             <div id="cell30">30</div>  
  44.         </div>  
  45.         <a href="#" id="btn1" class="button">Div Shuffle</a>  
  46.         <br>  
  47.         <a href="#" id="btn2" class="button">Background Theme</a>  
  48.         <br>  
  49.         <a href="#" id="btn3" class="button">Add and Remove Class</a>  
  50.         <br>  
  51.         <a href="#" id="btn4" class="button">Reload</a>  
  52.     </form>  
  53. </body>  
  54.   
  55. </html>  
The preceding HTML code defines the div element groups and their child elements. Use the href attribute tag to add multiple buttons.

CSS

Use CSS code to make the look and feel for all the HTML elements as we used before in the HTML part. The following is the CSS code I used in this tutorial.
  1. @  
  2. import url(http: //fonts.googleapis.com/css?family=Varela+Round);  
  3.         body {  
  4.             background - color: DodgerBlue;  
  5.         }  
  6.         form {  
  7.             margin - left: 25 % ;  
  8.             margin - right: 25 % ;  
  9.             width100 % ;  
  10.         }  
  11.         .highlight {  
  12.             backgroundgreen;  
  13.             opacity: 0.3;  
  14.             filter: alpha(opacity = 1);  
  15.         }#  
  16.         shuffle > div {  
  17.             floatleft;  
  18.             text - align: center;  
  19.             line - height50 px;  
  20.             width50 px;  
  21.             height50 px;  
  22.             color: rgba(0000.5);  
  23.             background - color: Gold;  
  24.             border - radius: 5 px;  
  25.             margin5 px;  
  26.         }  
  27.   
  28.         #  
  29.         shuffle {  
  30.             background - color: DodgerBlue;  
  31.             max - width100 px;  
  32.             line - height40 px  
  33.             height360 px;  
  34.             floatleft;  
  35.             font - size16 pt;  
  36.             filter: alpha(opacity = 40);  
  37.             padding10 px;  
  38.             max - width300 px;  
  39.         }  
  40.         .button {  
  41.             border1 px# E1AD00 solid; - webkit - border - radius: .25e m; - moz - border - radius: .25e m;  
  42.             border - radius: .25e m;  
  43.             displayinline - block;  
  44.             color#7B5F00;  
  45. font-size: .9em;  
  46. background: # F4D03F;  
  47.             background: -moz - linear - gradient(top#FFF2C7 0#FFD548 4 % , #FAC100 100 % );  
  48.             background: -webkit - gradient(linear, left topleft bottom, color - stop(0 % , #FFF2C7), color - stop(4 % , #FFD548), color - stop(100 % , #FAC100));  
  49.             background: -webkit - linear - gradient(top#FFF2C7 0#FFD548 4 % , #FAC100 100 % );  
  50.             background: -o - linear - gradient(top#FFF2C7 0#FFD548 4 % , #FAC100 100 % );  
  51.             background: -ms - linear - gradient(top#FFF2C7 0#FFD548 4 % , #FAC100 100 % );  
  52.             background: linear - gradient(to bottom#FFF2C7 0#FFD548 4 % , #FAC100 100 % );  
  53.             filter: progid: dximagetransform.microsoft.gradient(startColorstr = '@color', endColorstr = 'darken(@color, 15%)', GradientType = 0);  
  54.             padding8 px 14 px;  
  55.             positionrelative;  
  56.             text - align: center;  
  57.             text - decoration: none;  
  58.             text - shadow: 0 1 px 0 rgba(2552552550.25);  
  59.             font - size15 px;  
  60.             font - family: 'Helvetica Neue',  
  61.             Helvetica,  
  62.             Arial,  
  63.             sans - serif;  
  64.             margin2e m;  
  65.         }  
  66.   
  67.         .button: active {  
  68.             background#F5D76E;  
  69.             border - color#AE8600# E1AD00# E1AD00;  
  70.             padding9 px 14 px 7 px;  
  71.             top: auto;  
  72.         }  
Before adding the CSS code, be sure it covers the entire browser display area. The preceding CSS code defines the entire HTML element design process.

JQuery scripting

The first step in the jQuery scripting part is to include the required jQuery library reference in the head element of your page.
  1. <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>   
The following is the complete jQuery and JavaScript code I used in this tutorial.
  1. <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>  
  2. <script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>  
  3. <script type="text/javascript">  
  4.     $(document).ready(function() {  
  5.         $("#shuffle div").click(function(e) {  
  6.             var Original = ($(this).attr("id"));  
  7.             alert("You Clicked : " + Original)  
  8.         });  
  9.         $("#shuffle div").hover(function() {  
  10.             $(this).css('cursor''pointer');  
  11.         }, function() {  
  12.             $(this).css('cursor''auto');  
  13.         });  
  14.         $('#btn1').click(function() {  
  15.             var parent = $("#shuffle");  
  16.             var divs = parent.children();  
  17.             while (divs.length) {  
  18.                 parent.append(divs.splice(Math.floor(Math.random() * divs.length), 1)[0]);  
  19.             }  
  20.         });  
  21.   
  22.         function get_random_color() {  
  23.             var letters = '0123456789ABCDEF'.split('');  
  24.             var color = '#';  
  25.             for (var i = 0; i < 6; i++) {  
  26.                 color += letters[Math.round(Math.random() * 15)];  
  27.             }  
  28.             return color;  
  29.         }  
  30.         $('#btn2').click(function() {  
  31.             $("body,#shuffle").css("background-color", get_random_color());  
  32.         });  
  33.         $('#btn3').click(function() {  
  34.             var $divs = $("#shuffle > div ").removeClass('highlight');  
  35.             rnd = Math.floor(Math.random() * $divs.length);  
  36.             $divs.eq(rnd).addClass('highlight');  
  37.         });  
  38.         $('#btn4').click(function() {  
  39.             location.reload();  
  40.         });  
  41.     });  
  42. </script>  
The preceding JQuery scripting code improves the performance of the application and defines the entire HTML element UI functionality and effects.

Complete code

The following is the complete HTML, CSS and JQuery scripting codes I used in this tutorial.
  1. <!DOCTYPE HTML>  
  2. <HTML>  
  3.   
  4. <HEAD>  
  5.     <TITLE>SAMPLE</TITLE>  
  6.     <style>  
  7.         @import url(http://fonts.googleapis.com/css?family=Varela+Round);  
  8.         body {  
  9.             background-color: DodgerBlue;  
  10.         }  
  11.           
  12.         form {  
  13.             margin-left25%;  
  14.             margin-right25%;  
  15.             width100%;  
  16.         }  
  17.           
  18.         .highlight {  
  19.             backgroundgreen;  
  20.             opacity: 0.3;  
  21.             filter: alpha(opacity=1);  
  22.         }  
  23.           
  24.         #shuffle > div {  
  25.             floatleft;  
  26.             text-aligncenter;  
  27.             line-height50px;  
  28.             width50px;  
  29.             height50px;  
  30.             color: rgba(0000.5);  
  31.             background-color: Gold;  
  32.             border-radius: 5px;  
  33.             margin5px;  
  34.         }  
  35.           
  36.         #shuffle {  
  37.             background-color: DodgerBlue;  
  38.             max-width100px;  
  39.             line-height40px height360px;  
  40.             floatleft;  
  41.             font-size16pt;  
  42.             filter: alpha(opacity=40);  
  43.             padding10px;  
  44.             max-width300px;  
  45.         }  
  46.           
  47.         .button {  
  48.             border1px #E1AD00 solid;  
  49.             -webkit-border-radius: .25em;  
  50.             -moz-border-radius: .25em;  
  51.             border-radius: .25em;  
  52.             display: inline-block;  
  53.             color#7B5F00;  
  54.             font-size: .9em;  
  55.             background#F4D03F;  
  56.             background: -moz-linear-gradient(top#FFF2C7 0#FFD548 4%, #FAC100 100%);  
  57.             background: -webkit-gradient(linear, left topleft bottom, color- stop(0%#FFF2C7), color-stop(4%#FFD548), color-stop(100%#FAC100));  
  58.             background: -webkit-linear-gradient(top#FFF2C7 0#FFD548 4%, #FAC100 100%);  
  59.             background: -o-linear-gradient(top#FFF2C7 0#FFD548 4%, #FAC100 100%);  
  60.             background: -ms-linear-gradient(top#FFF2C7 0#FFD548 4%, #FAC100 100%);  
  61.             background: linear-gradient(to bottom#FFF2C7 0#FFD548 4%, #FAC100 100%);  
  62.             filter: progid: dximagetransform.microsoft.gradient(startColorstr='@color', endColorstr='darken(@color, 15%)', GradientType=0);  
  63.             padding8px 14px;  
  64.             positionrelative;  
  65.             text-aligncenter;  
  66.             text-decorationnone;  
  67.             text-shadow0 1px 0 rgba(2552552550.25);  
  68.             font-size15px;  
  69.             font-family'Helvetica Neue'HelveticaArialsans-serif;  
  70.             margin2em;  
  71.         }  
  72.           
  73.         .button:active {  
  74.             background#F5D76E;  
  75.             border-color#AE8600 #E1AD00 #E1AD00;  
  76.             padding9px 14px 7px;  
  77.             top: auto;  
  78.         }  
  79.     </style>  
  80.     <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>  
  81.     <script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>  
  82.     <script type="text/javascript">  
  83.         $(document).ready(function() {  
  84.             $("#shuffle div").click(function(e) {  
  85.                 var Original = ($(this).attr("id"));  
  86.                 alert("You Clicked : " + Original)  
  87.             });  
  88.             $("#shuffle div").hover(function() {  
  89.                 $(this).css('cursor''pointer');  
  90.             }, function() {  
  91.                 $(this).css('cursor''auto');  
  92.             });  
  93.             $('#btn1').click(function() {  
  94.                 var parent = $("#shuffle");  
  95.                 var divs = parent.children();  
  96.                 while (divs.length) {  
  97.                     parent.append(divs.splice(Math.floor(Math.random() * divs.length), 1)[0]);  
  98.                 }  
  99.             });  
  100.   
  101.             function get_random_color() {  
  102.                 var letters = '0123456789ABCDEF'.split('');  
  103.                 var color = '#';  
  104.                 for (var i = 0; i < 6; i++) {  
  105.                     color += letters[Math.round(Math.random() * 15)];  
  106.                 }  
  107.                 return color;  
  108.             }  
  109.             $('#btn2').click(function() {  
  110.                 $("body,#shuffle").css("background-color", get_random_color());  
  111.             });  
  112.             $('#btn3').click(function() {  
  113.                 var $divs = $("#shuffle > div ").removeClass('highlight');  
  114.                 rnd = Math.floor(Math.random() * $divs.length);  
  115.                 $divs.eq(rnd).addClass('highlight');  
  116.             });  
  117.             $('#btn4').click(function() {  
  118.                 location.reload();  
  119.             });  
  120.         });  
  121.     </script>  
  122. </head>  
  123.   
  124. <body>  
  125.     <h1 align="center"><img class="banner" src="https://cdn2.iconfinder.com/data/icons/micon-social-pack/512/jquery-128.png"></h1>  
  126.     <form>  
  127.         <div id="shuffle">  
  128.             <div id="cell1">1</div>  
  129.             <div id="cell2">2</div>  
  130.             <div id="cell3">3</div>  
  131.             <div id="cell4">4</div>  
  132.             <div id="cell5">5</div>  
  133.             <div id="cell6">6</div>  
  134.             <div id="cell7">7</div>  
  135.             <div id="cell8">8</div>  
  136.             <div id="cell9">9</div>  
  137.             <div id="cell10">10</div>  
  138.             <div id="cell11">11</div>  
  139.             <div id="cell12">12</div>  
  140.             <div id="cell13">13</div>  
  141.             <div id="cell14">14</div>  
  142.             <div id="cell15">15</div>  
  143.             <div id="cell16">16</div>  
  144.             <div id="cell17">17</div>  
  145.             <div id="cell18">18</div>  
  146.             <div id="cell19">19</div>  
  147.             <div id="cell20">20</div>  
  148.             <div id="cell21">21</div>  
  149.             <div id="cell22">22</div>  
  150.             <div id="cell23">23</div>  
  151.             <div id="cell24">24</div>  
  152.             <div id="cell25">25</div>  
  153.             <div id="cell26">26</div>  
  154.             <div id="cell27">27</div>  
  155.             <div id="cell28">28</div>  
  156.             <div id="cell29">29</div>  
  157.             <div id="cell30">30</div>  
  158.         </div>  
  159.         <a href="#" id="btn1" class="button">Div Shuffle</a>  
  160.         <br>  
  161.         <a href="#" id="btn2" class="button">Background Theme</a>  
  162.         <br>  
  163.         <a href="#" id="btn3" class="button">Add and Remove Class</a>  
  164.         <br>  
  165.         <a href="#" id="btn4" class="button">Reload</a>  
  166.     </form>  
  167. </body>  
  168.   
  169. </html>  
Output

1. Open the main Index html file in the web browser.

Main index page
Figure 1: Main index page

2. Press the div element to get div value.

DIV element value
Figure 2: DIV element value

3. Press the div shuffle button to shuffle the div elements group.

div elements group
Figure 3: Shuffle div elements group

Shuffle div
Figure 4: Shuffle div elements group

4. Press background theme button to change theme.

Shuffle div elements
Figure 5: Shuffle div elements group

Shuffle div elements group
Figure 6: Shuffle div elements group

5. Press add and remove class button to add the class dynamically.

Add and remove class
Figure 7: Add and remove class dynamically

Add and remove class dynamically
Figure 8: Add and remove class dynamically

6. Press reload button to reset the page setup.

Reset the page setup
Figure 9: Reset the page setup

Conclusion

I hope you liked this useful article. It will be useful for jQuery beginners. Please provide your valuable feedback and suggestions.

Live demo link: Jsfiddle

References