Jquery Course

After we have selected the elements in jQuery we are able to manipulate them and working with CSS properties to build effects.

Reading CSS Properties

To read the CSS property of an element, use the following sintax:
$('selector').css('css_property');
- selector - represents a HTML element.
- css_property - is the CSS property you want to read.

The code in the following example gets the width and the background-color of the first <div> with class="cls", then displays these values in an Alert window when a button with id="btn" is clicked:
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<title>Read CSS styles</title>
<style type="text/css"><!--
.cls {
 width:250px;
 background-color:#bcefcd;
 border:2px solid blue;
}
--></style>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
<script type="text/javascript"><!--
$(document).ready(function() {
  // get the width and background-color of the first <div> with class="cls"
  var div1_width = $('div.cls:first').css('width');
  var div1_bgcolor = $('div.cls:first').css('background-color');

  // displays an Alert window when the HTML tag with id="btn" is clicked
  $('#btn').click(function(){
    alert('width = '+ div1_width+ ' , bgcolor = '+ div1_bgcolor);
  });
});
--></script>
</head>
<body>
<div class="cls">jQuery Tutorial</div>
<div class="cls">coursesweb.net/javascript/</div>
<button id="btn">Click</button>
</body>
</html>
Click on the button below to see the result.
jQuery Tutorial
coursesweb.net/javascript/

jQuery gives you the element's calculated style. You'll receive the value that's been rendered in the user's browser, rather than the value entered in the CSS definition. So, if you gave a div a height of 300 pixels in the CSS file, but the content inside it pushed the height over 300 pixels, jQuery would provide you with the actual height of the item, rather than the 300 pixels you'd specified.

Setting CSS Properties

To set a CSS property to an HTML element with jQuery, use the following syntax:
$('selector').css('css_property', 'value');
Example:
<script type="text/javascript"><!--
$(document).ready(function() {
  // sets the font-size of all <div> with class="cls", when the HTML tag with id="btn" is clicked
  $('#btn').click(function(){
    $('div.cls').css('font-size', '20px');
  });
});
--></script>
<div class="cls">jQuery Tutorial</div>
<div class="cls">coursesweb.net/javascript/</div>
<button id="btn">Click</button>
The code above sets the CSS property font-size:20px; to all <div> tags with class="cls", when the HTML tag with id="btn" is clicked.
Click on the button below to see the effect.
jQuery Tutorial
coursesweb.net/javascript/

If you want to set multiple CSS properties in a single statement, you can use the following syntax:
$('selector').css({ 'property1': 'value', 'property2': 'value' });
- You can add as many 'property':'value' pairs as you want, within the curly braces, separated by comma.

Example:
<script type="text/javascript"><!--
$(document).ready(function() {
  // sets multiple css properties to the <div> with class="cls", when it is clicked
  $('div.cls').click(function(){
    $(this).css({'margin': '3px auto', 'background': '#bcfecd', 'color': 'blue'});
  });
});
--></script>
<div class="cls">jQuery Tutorial</div>
<div class="cls">coursesweb.net/javascript/</div>
The code above sets this CSS properties: margin:2px auto; background':#bcfecd; color:blue; to any <div> with class="cls", when it is clicked.
To see the effect, click on these two text lines:
jQuery Tutorial
coursesweb.net/javascript/

Adding and Removing Classes

jQuery allows you to easily add, remove, and toggle CSS classes. Here are the different syntaxes for accomplishing this:

    $('selector').addClass('class_name');   - adds class "class_name" to all HTML elements that match "selector".
    $('selector').addClass('class_name1 class_name2');   - adds class "class_name1" and "class_name2" to all HTML elements that match "selector".
    $('selector').removeClass('class_name');   - removes class "class_name" from all HTML elements that match "selector".
    $('selector').toggleClass('class_name');   - toggles the class "class_name" on all elements that match "selector" (adds it if it doesn't exist, and removes it if it does).

Example:
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<title>Add, Remove class</title>
<style type="text/css"><!--
.cls1 { border:2px solid #01da02; color:blue; }
.cls2 { background:#bcfecd; font-size:18px; }
--></style>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
<script type="text/javascript"><!--
$(document).ready(function() {
  // adds class "cls2" to the tag with id="id1", and remove class "cls1" from all <div> tags
  // when the tag with id="btn" is clicked
  $('#btn').click(function(){
    $('#id1').addClass('cls2');
    $('div').removeClass('cls1');
  });
});
--></script>
</head>
<body>
<div class="cls1">Free Web Development Courses</div>
<div id="id1">Web site: marplo.net</div>
<button id="btn">Click</button>
</body>
</html>
The jQuery code in this example adds class "cls2" to the tag with id="id1", and removes class "cls1" from all <div> tags, when the element with id="btn" is clicked.
Click on the button below to see the efect:
Free Web Development Courses
Web site: marplo.net

Check if a class is set

You can also check to see if a selected element has a particular CSS class, by using the hasClass() method, and then run some code if it does. Here is the syntax:
if ($('anElement').hasClass('class_name')) {
  // do something here
}
- If the HTML element specified by "anElement" has a class of "class_name", the browser will execute the code within curly braces.

Example:
<script type="text/javascript"><!--
$(document).ready(function() {
  // when the mouse is over a DIV with a class of "cls", adds a border to it
  $('div').mouseover(function() {
    if($(this).hasClass('cls')) {
      $(this).css('border', '2px solid blue');
    }
  });
});
--></script>
<div class="cls">Div with class="cls"</div>
<div>DIV without class</div>
<div class="cls">Another div with class="cls"</div>
The code above adds a border to each DIV with a class of "cls", when the mouse is over it.
To see the result, place the mouse over these text lines:
Div with class="cls"
DIV without class
Another div with class="cls"

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which tag adds an image in web page?
<div> <img> <span>
<img src="http://coursesweb.net/imgs/webcourses.gif" width="191" height="63" alt="Courses-Web" />
Which of these CSS codes displays the text oblique?
font-style: italic; text-decoration: underline; font-weight: 500;
#id {
  font-style: italic;
}
Click on the jQuery function used to hide with animation a HTML element.
click() hide() show()
$(document).ready(function() {
  $(".a_class").click(function(){ $(this).hide("slow"); });
});
Click on the correctly defined function in PHP.
fname function() {} function fname() {} function $fname() {};
function fname($a, $b) {
  echo $a * $b;
}
CSS Styles with jQuery

Last accessed pages

  1. Upload Script for Gallery of Images and Audio files (9696)
  2. Read Excel file data in PHP - PhpExcelReader (96716)
  3. The Power of Positive Thinking (510)
  4. Textarea with buttons to format text, colors and smiles (5222)
  5. Dynamically Button to Scroll to Page Top (1181)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (320)
  2. Read Excel file data in PHP - PhpExcelReader (117)
  3. The Four Agreements (96)
  4. PHP Unzipper - Extract Zip, Rar Archives (91)
  5. The Mastery of Love (85)
Chat
Chat or leave a message for the other users
Full screenInchide