Search 
DailyCoding > Design

Customized Html Controls: Creating Custom Checkbox

Discuss about creating custom html checkbox under customized html controls.
Author admin on Dec 17, 2008 8 Comments
Rate it    (Rated 3 by 41 people)
7,536 Views

Under “Customized Html Controls” section I will discuss about how to create and customized standard html controls. This is first article in this category. This will discuss about how we can create a custom checkbox using CSS and jQuery.

Checkbox Image

For this you need two images, one for checked state of checkbox and another for unchecked state of the checkbox. However, you can also use a single image containing both checked and unchecked state and use CSS trick like I use.

Checkbox CSS

Here is the CSS classes we will be going to use for the checkbox

.checkBox
{
    background-position: 0px 0px;
}

.checkBoxClear
{
    background-position: -21px 0px;
}

.checkBox, .checkBoxClear
{
    background-image: url('CheckBox.png');
    background-repeat: no-repeat;
    display: inline-block;
    float: left;
    width: 21px;
    height: 21px;
    padding: 0px;
    margin: 0px;
    cursor: hand;
}

Checkbox JavaScript

Finally below is the JavaScript code using jQuery which will control the checking and un-checking of our checkbox using click event. It will basically toggle the CSS which will change the state.

<script language="javascript" type="text/javascript">
<!--
$(document).ready(function()
{
    $(".checkBox,.checkBoxClear").click(function(srcc)
    {
        if ($(this).hasClass("checkBox"))
        {
            $(this).removeClass("checkBox");
            $(this).addClass("checkBoxClear");
        }
        else
        {
            $(this).removeClass("checkBoxClear");
            $(this).addClass("checkBox");
        }
    });        
});
//-->
</script>

Html Code

Here is a sample Custom Checkbox with the html code.

Select options

 

 
 
 

<h3>Select options</h3>
<div id="Div1" class="checkBox">&nbsp;</div>
<label for="Div1">
    Lorem ipsum dolor sit amet</label><br />
<div id="Div2" class="checkBox">
    &nbsp;</div>
<label for="Div2">
    Aenean vitae elit quis erat interdum tempus</label>
<div id="Div3" class="checkBox">
    &nbsp;</div>
<label for="Div3">
    Duis laoreet viverra quam</label>
<div id="Div4" class="checkBox">
    &nbsp;</div>
<label for="Div4">
    Mauris pellentesque tristique erat</label>
CSS | Html | Javascript | jQuery

Discussion

Adrian Callaghan On Feb 16, 2009 03:54 AM
Thanks for a nice tutorial

sivagurunathans On Apr 28, 2009 08:50 PM
Nice Trick

fdfd On Apr 30, 2009 11:28 PM

kcvg On May 1, 2009 09:00 PM
Checkbox CSS:
I think “cursor: hand” Replaced “cursor:pointer; ” Will be better!
???

karen On Jun 21, 2009 12:34 AM
thanks, but id doesn't work properly. what do I do?

jkk On Aug 13, 2009 05:47 AM
How to make it default unchecked?

Daily Coder On Aug 13, 2009 05:54 AM
@jkk, make its class="checkBoxClear"

zengzhan On Nov 11, 2009 06:00 PM
If use input = "checkbox" is better,I continue to look forward to

Leave a Comment

Name
Email Address
Web Site
© Copyright 2008 Daily Coding • All rights reserved