Wednesday, January 30, 2013

Check Box and Jquery

I was searching for code snippet to check status of checkbox through jquery, Here is what I figured out and it is very intresting.
Following is the Jquery you need to place in the <script> tag

<script>
$(document).ready(function()
{
$("#button2").click(function()
{
var isChecked = $("#check_1").attr('checked')?true:false;
if(isChecked)
{
$("#check_1").prop('checked',false);
}
else
{
$("#check_1").prop('checked',true);
}
}
);
}
);
</script>
Now HTML part
  • <input type="checkbox" id="check_1" ><\li>
  • <button type="button" id="button2" >I am button 2<button><\li>

Description of HTML Part


<input type="checkbox" id="check_1" >
we take Input type:checkbox and we will change its status on button click

<button type="button" id="button2" >I am button 2<button>
We define a button with the id button2. When we click the button this will change the checkbox status using above Jquery in <script> tag.

Jquery Description


$(document).ready(function()
The above line waits for the document to get downloaded.

$("#button2").click(function()
The above line detects if there is click on the button2

var isChecked = $("#check_1").attr('checked')?true:false;
if(isChecked)
{
$("#check_1").prop('checked',false);
}
else
{
$("#check_1").prop('checked',true);
}

The above code is executed when the button is clicked

$("#check_1").attr('checked')?true:false;
returns the current state of checkbox.

$("#check_1").prop('checked',false);
The above line makes the state false if checkbox is already checked.

$("#check_1").prop('checked',true);
The above line makes the state true if checkbox is already checked.

Monday, January 28, 2013

HTML5 CANVAS

CANVAS is newly introduced in HTML5. CANVAS is used to render graphics on the fly.

The most common usage are as below


  • It can be used to draw graphics.
  • Make photo compositions.
  • Create animations or even do real-time video processing.



Now let us see the Canvas Element Tag:
<canvas id="canvas1" width="100px" height="100px"> </canvas>

We can have more than one canvas in HTML page. Each can be accessed through their unique (id) attribute. Now the <canvas> can be accessed through DOM as shown below.

<canvas id="canvas_1" width="100px" height="100px"> </canvas>
var vcanvas= document.getElementById("canvas_1");

The canvas is border less. If width and height are not specified it will be 300 pixels wide and 150 pixels high. let us do some examples with <canvas>.style is used in <canvas> to define border

<! Doctype HTML>
<HTML>
<Body>
<canvas id="canvas_1" width="100px" height="100px" style="border:1px solid #000000;"> </canvas>
</Body> </HTML>

Javascript is required to draw something in <canvas>


A button tag is introduced to call the javascript function DrawRectangle().
<button type="button" id="button1" onclick="DarwRectangle()">Click Me</button>

<script>
function DarwRectangle()
{
var canvas= document.getElementById("canvas_1");
var ctx= canvas.getContext("2d");
ctx.fillStyle="#FF0000";
ctx.fillRect(0,0,150,75);
}
</script>
The Output will be like following.

Explanation of the function DrawRectangle()

document.getElementById("canvas_1");
The above line returns the canvas object.

canvas.getContext("2d");

The getContext("2d") object is a built-in HTML5 object, with many properties and methods for drawing paths, boxes, circles, text, images, and more.

ctx.fillStyle="#FF0000";
ctx.fillRect(0,0,150,75);

The fillStyle property can be a CSS color,
a gradient, or a pattern. The default fillStyle is #000000 (black).

The fillRect(x,y,width,height) method draws a rectangle
filled with the current fill style.

More on canvas in Part 2 continued....

Wednesday, January 23, 2013

HTML5 Elements

New Elements in HTML5


I was going through my paper 70-480 so i thought to share on my Blog what new and exciting features are introduced in HTML5.

The Last version of HTML was 4.01 and it was introduced in 1999 and since then web has come a long way.

Following are new elements introduces in HTML5.

New Semantic \ Structural Elements

Tag Description
<article> Defines an article
<aside> Defines content aside from the page content
<bdi> Isolates a part of text that might be formatted in a different direction from other text outside it
<command> Defines a command button that a user can invoke
<details> Defines additional details that the user can view or hide
<summary> Defines a visible heading for a <details> element
<figure> Specifies self-contained content, like illustrations, diagrams, photos, code listings, etc.
<figcaption> Defines a caption for a <figure> element
<footer> Defines a footer for a document or section
<header> Defines a header for a document or section
<hgroup> Groups a set of <h1> to <h6> elements when a heading has multiple levels
<mark> Defines marked/highlighted text
<meter> Defines a scalar measurement within a known range (a gauge)
<nav> Defines navigation links
<progress> Represents the progress of a task
<ruby> Defines a ruby annotation (for East Asian typography)
<rt> Defines an explanation/pronunciation of characters (for East Asian typography)
<rp> Defines what to show in browsers that do not support ruby annotations
<section> Defines a section in a document
<time> Defines a date/time
<wbr> Defines a possible line-break


New Media Elements

HTML5 offers new elements for media content:

<audio> Defines sound content
<video> Defines a video or movie
<source> Defines multiple media resources for <video> and <audio>
<embed> Defines a container for an external application or interactive content (a plug-in)
<track> Defines text tracks for <video> and <audio>

Tuesday, January 22, 2013

HTML5 localStorage and sessionStorage

The localStorage and sessionStorge from HTML5 are very helpful
in in saving data in Key/Value pair and provide a replacement
for our old buddy Cookies.

Now lets see it basics

HTML5 Storage Object Methods

1) getItems(key)--> key is the sting value.
2) setItems(key)--> key is the sting value.
3) removeItem(key) key is the sting value.
4) key(n) --> n is the name of the key and return type is value of that key.
5) clear().


Explanation for localStorage.

We have a div with the name "result".
we set its text (innerHtml) to Last Name:in the script section.
typeof(Storage) is used to check if the browser support Storage.

1) localStorage.setItem('lastname','Smith'); is used to set the values.

2) document.getElementById("result").innerHTML="Last Name:" + localStorage.lastname; is used to set the values in div.

3) localStorage.clear(); is used to clear all the keys and vlues in localStorage.


The same is with sessionStorage the only difference is it will loose values when the browser session is closed.