onlinecode

create custom checkboxes in HTML using CSS and jQuery

create custom checkboxes in HTML using CSS and jQuery

In this post we will give you information about create custom checkboxes in HTML using CSS and jQuery. Hear we will give you detail about create custom checkboxes in HTML using CSS and jQueryAnd how to use it also give you demo for it if it is necessary.

Use the CSS :checked Pseudo-class with jQuery


If you try to customize the checkboxes directly using the CSS properties like background or border it will not produce the desired result, because most of the form elements are native part of the browsers and does not accepts most of the visual styling. So we have to find another way to style checkboxes.


In this tutorial we will create custom checkboxes that appears consistent across the browsers with the help of CSS and little bit of jQuery. There are many jQuery plug-ins available to customize checkbox elements but they are too complex and not so easy to integrate.


This solution will give you the full control over the style and placement of the checkboxes and very easy to implement in any project. Also, it is highly compatible across the browsers and work seamlessly even on the older version of the Internet Explorer such as IE7.


<form action="action.php" method="post">
    <h3>Choose Your Favorite Sports</h3>
    <label><input type="checkbox" name="sport[]" checked="checked" value="football"> Football</label>
    <label><input type="checkbox" name="sport[]" value="cricket"> Cricket</label>
    <label><input type="checkbox" name="sport[]" value="baseball"> Baseball</label>
    <label><input type="checkbox" name="sport[]" value="tennis"> Tennis</label>
    <label><input type="checkbox" name="sport[]" value="basketball"> Basketball</label>
</form>


When you call the customCheckbox() function it will wrap the <input> element with a <span> element and apply the .custom-checkbox class on it. Once the .custom-checkbox class is applied to the <span> element CSS will hide the default checkbox and show the image of custom checkbox in place of them. Don’t forget to include the jQuery file in your document before this script. We have called the customCheckbox() function when DOM is fully loaded.


<script>
    function customCheckbox(checkboxName){
        var checkBox = $('input[name="'+ checkboxName +'"]');
        $(checkBox).each(function(){
            $(this).wrap( "<span class='custom-checkbox'></span>" );
            if($(this).is(':checked')){
                $(this).parent().addClass("selected");
            }
        });
        $(checkBox).click(function(){
            $(this).parent().toggleClass("selected");
        });
    }
    $(document).ready(function (){
        customCheckbox("sport[]");
    })
</script>


 

Hope this and post will helped you for implement create custom checkboxes in HTML using CSS and jQuery. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and Keep reading our blogs

For More Info See :: laravel And github

Exit mobile version