Wednesday, February 6, 2013

How to set one or more properties for the set of matched elements using jQuery?

To set one or more properties for the set of matched elements, we can make use of the ".prop(propertyName,value)" method in jQuery. Where "propertyName" is the name of the property to set and "value" is the value of the property to be set.

Using ".prop(propertyName,value)" method to set the value of properties is the most convenient way, especially while setting multiple properties.

Note: Properties generally affect the dynamic state of a DOM element without changing the serialized HTML attribute.

Set the value of a property using ".prop(propertyName, value)" method:

$(document).ready(function(){
$("#checkAll").click(function(){
$("input[type='checkbox']").prop("checked", true)
});
$("#unCheckAll").click(function(){
$("input[type='checkbox']").prop("checked", false)
});
$("#toggleAll").click(function(){
$("input[type='checkbox']").prop("checked", function( i, val ) {
  return !val;
});
});
});

Example:

<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
$(document).ready(function(){
$("#checkAll").click(function(){
$("input[type='checkbox']").prop("checked", true)
});
$("#unCheckAll").click(function(){
$("input[type='checkbox']").prop("checked", false)
});
$("#toggleAll").click(function(){
$("input[type='checkbox']").prop("checked", function( i, val ) {
  return !val;
});
});
});
</script>
</head>
<body>
<b>Click on the below buttons to see the usage of ".prop" method:</b><br/>
<input type="button" id="checkAll" value="Check all">
<input type="button" id="unCheckAll" value="Uncheck all">
<input type="button" id="toggleAll" value="Toggle all"></br></br>
<input id="checkbox1" type="checkbox" checked="checked"><label for="checkbox1">Check/Uncheck me</label></br></br>
<input id="checkbox2" type="checkbox" ><label for="checkbox2">Check/Uncheck me</label></br></br>
<input id="checkbox3" type="checkbox" checked="checked"><label for="checkbox3">Check/Uncheck me</label></br></br>
<input id="checkbox4" type="checkbox" ><label for="checkbox4">Check/Uncheck me</label></br></br>
<div></div>
</body>
</html>

Sample Output:


How to set one or more properties for the set of matched elements using jQuery

Demo:



Tuesday, February 5, 2013

How to get the value of a property for the first matched element using jQuery?

To calculate the value of an property for the first matched we can make use of the ".prop()" method in jQuery.

With the help of ".prop()" method we can calculate the value of the property only for first matched element. In order to calculate the property value for each element individually, we should use a looping construct like ".each()" or ".map()" method of jQuery.

Note: If the properties are not set on the elements, then the ".prop()" method will return "undefined".

Here we need to understand the difference between ".attr()" and ".prop" methods of jQuery. Below is a sample value which we get, when we use the methods ".attr()" and ".prop" on a "checked" attribute of a checkbox.

         for the code ".attr('checked')" output is "checked".
         for the code ".prop('checked')" output is "true".

Get the value of a property using ".prop()" method:

$(document).ready(function(){
$("input").change(function() {
  var checkProperty = $(this);
  $("div").html(".attr('checked'): <b>" + checkProperty.attr('checked') + "</b><br>"
              + ".prop('checked'): <b>" + checkProperty.prop('checked')) + "</b><br>";
}).change();
});

Example:

<!DOCTYPE html>
<html>
<head>
<style>     
 body {background:#F1F1A8;} 
</style>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
$(document).ready(function(){
$("input").change(function() {
  var checkProperty = $(this);
  $("div").html(".attr('checked'): <b>" + checkProperty.attr('checked') + "</b><br>"
              + ".prop('checked'): <b>" + checkProperty.prop('checked')) + "</b><br>";
}).change();
});
</script>
</head>
<body>
<b>Click on the below checkbox to see the usage of ".attr()" and ".prop" methods:</b><br/>
<input id="checkbox1" type="checkbox" checked="checked">
<label for="checkbox1">Check/Uncheck me</label></br></br>
<div></div>
</body>
</html>

Sample Output:


How to get the value of a property for the first matched element using jQuery

Demo:



Friday, February 1, 2013

How to dynamically insert html code/content into a web page using jQuery?

To dynamically insert html code/content into a web page, we can use ".html(htmlstring)" method of jQuery. Where "htmlstring" is a HTML string which is to be set as content of each and every matched elements.

When we use the method ".html(htmlstring)" to set any element's content, it will not only replace complete content in that element with the new content but also removes other constructs like data and event handlers from child elements before replacing new content.

Internally jQuery uses the browsers "innerHTML" property for this functionality.

Note: We cannot use this method for XML documents.

Inject html code/content using ".html(htmlstring)" method:

$(document).ready(function(){
$("div.button").click(function () {
$(this).html("<input type='button'; value='Button'>");
});
$("div.checkbox").click(function () {
$(this).html("<input type='checkbox' name='check' value='check'>Check Box</input>");
});
$("div.radiobutton").click(function () {
$(this).html("<input type='radio' name='radio' value='radio'>Radio Button</input>");
});
$("div.textbox").click(function () {
$(this).html("<input type='text' name='text' value='text'>Text box</input>");
});
});

Example:

<!DOCTYPE html>
<html>
<head>
<style>     
  div {padding:0px;margin:0px;cursor:pointer;}
</style>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
$(document).ready(function(){
$("div.button").click(function () {
$(this).html("<input type='button'; value='Button'>");
});
$("div.checkbox").click(function () {
$(this).html("<input type='checkbox' name='check' value='check'>Check Box</input>");
});
$("div.radiobutton").click(function () {
$(this).html("<input type='radio' name='radio' value='radio'>Radio Button</input>");
});
$("div.textbox").click(function () {
$(this).html("<input type='text' name='text' value='text'>Text box</input>");
});
});
</script>
</head>
<body>
<b>Click on the below items to insert html elements:</b><br/>
<div class="button">Click here to insert a Button.</div>
<div class="checkbox">Click here to insert a Checkbox.</div>
<div class="radiobutton">Click here to insert a Radio button.</div>
<div class="textbox">Click here to insert a Textbox.</div>
</body>
</html>

Sample Output:


How to dynamically insert html code/content into a web page using jQuery

Demo:



Thursday, January 31, 2013

How to get the html code/content of any element in a web page using jQuery?

To retrieve the html code/content of any element in a webpage, we can use ".html()" method of jQuery. We get the contents of the first matched element in the set of matched elements(it means, if the selector matches more than one element at a time, it will only return the html content of the first matched element).

Internally jQuery uses the browsers "innerHTML" property for this functionality.

Note: We cannot use this method for XML documents.

Get html code/content using ".html()" method:

$(document).ready(function(){ 
$("div").click(function (e) {
var htmlCode = $(this).html();
e.stopPropagation();
$(this).text(htmlCode);
});
});

Example:

<!DOCTYPE html>
<html>
<head>
<style> 
  body {background:#F1F1A8;}   
  div,article,aside,span,p {padding:0px;margin:0px;cursor:pointer;} 
</style>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
$(document).ready(function(){ 
$("div").click(function (e) {
var htmlCode = $(this).html();
e.stopPropagation();
$(this).text(htmlCode);
});
});
</script>
</head>
<body>
<b>Click(once or more than once) on any of the below elements to find its html code:</b><br/>
<div><article>This is a article tag.</article></div>
<div><aside>This is a aside tag.</aside></div>
<div><span>This is a span tag.</span></div>
<div><p>This is a p tag.</p></div>
</body>
</html>

Sample Output:


How to get the html code/content of any element in a web page using jQuery

Demo:



Wednesday, January 30, 2013

How to set width to any element using jQuery?

To set width to any element we can make use of the ".width(value)" method of jQuery. Where  "value" can be any integerrepresenting the number of pixels (for example, 60) or an integer  with unit of measurement appended to it (for example, 60px or 60% or auto).

1. How to calculate width of the first matched element using jQuery?
2. How to set width to any element using jQuery?(this article)

Set width to element using "width()" method:

$(document).ready(function(){   
$("div.one").width(400).append("Width of this container is :"+$("div.one").width()+" px");
$("div.two").width(425).append("Width of this container is :"+$("div.two").width()+" px");
$("div.three").width(450).append("Width of this container is :"+$("div.three").width()+" px");
});

Example:

<!DOCTYPE html>
<html>
<head>
<style> 
  body {background:#F1F1A8;}   
  div {height:50px;background:red;margin:5px;} 
</style>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
$(document).ready(function(){   
$("div.one").width(400).append("Width of this container is :"+$("div.one").width()+" px");
$("div.two").width(425).append("Width of this container is :"+$("div.two").width()+" px");
$("div.three").width(450).append("Width of this container is :"+$("div.three").width()+" px");
});
</script>
</head>
<body>
  <div class="one"></div>
  <div class="two"></div>
  <div class="three"></div>
</body>
</html>

Sample Output:


How to set width to any element using jQuery

Demo:



Sunday, January 27, 2013

Top list of jQuery plugins for zooming images


1. AnythingZoomer

When we hover our mouse on a region/part of an image, a small area pops up giving a zoomed view of that small area.
Top list of jQuery plugins for zooming images


2. imgAreaSelect

To have a cropping functionality on images or selecting a rectangular portion of an image.
Top list of jQuery plugins for zooming images


3. Image Zoom and Clip Effect

Select a region or part of an image and then the image would automatically scale up to show the entire selected region.
Top list of jQuery plugins for zooming images


4. PhotoShoot

To convert any div container on our webpage into a Photo shooting effect.
Top list of jQuery plugins for zooming images


5. pan view

It allows visitors to have a pan view or details of any image.

6. Hover Zoom

Reverse zooming an image while fading, when mouse is hovered on it and view the details of that image.
Top list of jQuery plugins for zooming images


7. Power Zoomer

When mouse is hovered on a region/part of an image, that part of the image is shown in a magnifying glass.
Top list of jQuery plugins for zooming images


Tuesday, January 15, 2013

How to calculate width of the first matched element using jQuery?

To calculate width of the first matched element or window or document we can make use of the ".width()" method of jQuery.

We can also calculate width using ".css('width')" method of jQuery. Difference between .css('width') and .width() is, ".css('width')" will return a value with units appended to it (for example, 667px) while ".width()" will return a unit-less pixel value (for example, 667).

Now question is when to use which method? It is recommended to use ".width()" method when the calculated width needs to be used in a mathematical calculation.

1. How to calculate width of the first matched element using jQuery?(this article)
2. How to calculate width of any element including padding using jQuery?
3. How to calculate width of any element including padding, border and margin using jQuery?

Get width of element using "width()" method:

$(document).ready(function(){ 
  $("p").append($("p").width()+" px");
  $("div.divContainer").append($("div.divContainer").width()+" px");
  $("div.document").append($(document).width()+" px");
  $("div.window").append($(window).width()+" px");       
});

Example:

<!DOCTYPE html>
<html>
<head>
<style>
  body {background:#F1F1A8;} 
  .divContainer{width:550px;border:2px red solid;}
  p{width:530px;border:2px red solid;}
</style>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
$(document).ready(function(){ 
  $("p").append($("p").width()+" px");
  $("div.divContainer").append($("div.divContainer").width()+" px");
  $("div.document").append($(document).width()+" px");
  $("div.window").append($(window).width()+" px");       
});
</script>
</head>
<body>
<div>
  <div class="document">Width of this document is: </div>
  <div class="window">Width of this window is: </div>
  <div class="divContainer">Width of this div container is: </div>
  <p>Width of this paragraph is: </p>   
</div>
</body>
</html>

Sample output:


How to calculate width of the first matched element using jQuery


Demo:



How to toggle(add/remove) a CSS class selector dynamically using jQuery?

To add or remove single or multiple CSS class selectors from each of the set of matched elements, we can make use of the ".toggleClass()" method of jQuery.

We can pass one or more CSS class selectors as parameter to the method ".toggleClass()", each class selector being separated by space.

While this method is being applied, If an element in the set of matched elements already has a class specified name, then it is removed. If an element in the set of matched elements does not have the specified class name, then it is added.

Toggle single class selector using ".toggleClass()" method:

$(document).ready(function(){
$("p").click(function () {
   $(this).toggleClass("highlightThis");
});
});

Toggle multiple class selectors using ".toggleClass()" method:

$(document).ready(function(){
$("p").click(function () {
   $(this).toggleClass("myClass highlightThis");
});
});

Example:

<!DOCTYPE html>
<html>
<head>
<style>
  p{padding:0;margin:0;cursor:pointer;}   
  .myClass{background:yellow;color:red;font-size:16px}
  .highlightThis{background:red;font-weight:bold;font-size:20px}
</style>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
$(document).ready(function(){
$("p").click(function () {
   $(this).toggleClass("myClass highlightThis");
});
});
</script>
</head>
<body>
<div>
<p class="highlightThis">Click here to toggle style properties of this paragraph!!!!</p>
<p class="myClass">Click here to toggle style properties of this paragraph!!!!</p>
<p class="myClass">Click here to toggle style properties of this paragraph!!!!</p>
<p class="myClass">Click here to toggle style properties of this paragraph!!!!</p>
</div>
</body>
</html>

Sample output:


How to toggle(add/remove) a CSS class selector dynamically using jQuery

Demo:



Monday, January 14, 2013

How to set the vertical scroll bar position for each of the set of matched elements using jQuery?

To set the vertical scroll bar position for each of the set of matched elements we can make use of the ".scrollTop(value)" method of jQuery. Here "value" is an integer value, which represents the new vertical scroll bar position.

Vertical scroll position is equal to the total number of pixels that are hidden from view to the top of the scrollable area. Its value will be zero, either if the scroll bar is at very top, or if the element is not scrollable.

Setting vertical position of the scroll bar using ".scrollTop(value)" method:

$(document).ready(function(){
$("div#mainBlock").scrollTop(300);
});

Example:

<!DOCTYPE html>
<html>
<head>
<style> 
  div#mainBlock{
  border:3px solid black;
  width:590px;
  height:110px;
  overflow:auto;
  }
  div#contentBlock{
  background:#F1F1A8; 
  width:590px;
  height:790px;
  } 
  p{ 
  position:relative;
  Top:300px;
  font-weight:bold;}
</style>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
$(document).ready(function(){
$("div#mainBlock").scrollTop(300);
});
</script>
</head>
<body>
<div id="mainBlock">
<div id="contentBlock">
<p>I am at 300px to the Top of this container.</p>
</div>
</div>
</body>
</html>

Sample output:


How to set the vertical scroll bar position for each of the set of matched elements using jQuery

Demo:



How to calculate the current vertical position of the scroll bar for the first matched element using jQuery?

To calculate the current vertical position of the scroll bar for the first matched element we can make use of the ".scrollTop()" method of jQuery. ".scrollTop()" method returns an object which consists of scrollTop value in pixels.

Vertical scroll position is equal to the total number of pixels that are hidden from view to the Top of the scrollable area. Its value will be zero, either if the scroll bar is at very Top, or if the element is not scrollable.

Calculate vertical position of the scroll bar using ".scrollTop()" method:

$(document).ready(function(){
$("div#main").click(function (e) {
var verticalScroll = $("div#main").scrollTop();
$("#result span").text(verticalScroll+" pixels");
});
});

Example:

<!DOCTYPE html>
<html>
<head>
<style> 
  div#main{
  border:5px solid black;
  width:600px;
  height:100px;
  overflow:auto;
  }
  div#content{
  background:#F1F1A8 scroll; 
  width:600px;
  height:800px;
  }
  div#result{ 
  font-weight:bold;
  }
</style>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
$(document).ready(function(){
$("div#main").click(function (e) {
var verticalScroll = $("div#main").scrollTop();
$("#result span").text(verticalScroll+" pixels");
});
});
</script>
</head>
<body>
<div id="main">
<div id="content">
clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!! clickhere!!!!
</div>
</div>
<div id="result">Current Vertical scroll bar position of this div is: <span></span></div>
</body>
</html>

Sample output:


How to calculate the current vertical position of the scroll bar for the first matched element using jQuery

Demo:



Subscribe via Email