2

How to create custom checkboxes in HTML using CSS and jQuery

 1 year ago
source link: https://www.laravelcode.com/post/how-to-create-custom-checkboxes-in-html-using-css-and-jquery
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

How to create custom checkboxes in HTML using CSS and jQuery

  1773 views

  2 years ago

jQuery

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>
Author : Harsukh Makwana
Harsukh Makwana

Hi, My name is Harsukh Makwana. i have been work with many programming language like php, python, javascript, node, react, anguler, etc.. since last 5 year. if you have any issue or want me hire then contact me on [email protected]


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK