Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 880923
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T12:10:38+00:00 2026-05-15T12:10:38+00:00

I’m creating a small jQuery plugin for my CMS that styles certain form input

  • 0

I’m creating a small jQuery plugin for my CMS that styles certain form input types (just radio, checkbox at the moment). It works by hiding the original form element and placing a normal HTML element (for styling with CSS) in the input’s place. It then detects actions on the element and updates the original input accordingly. Additionally, it will also work when the associated label is clicked. Here is my code:

jQuery.fn.ioForm = function() {
    return this.each(function(){
        //For each input element within the selector.
        $('input', this).each(function() {
            var type = $(this).attr('type');

            //BOF: Radios and checkboxes.
            if (type == 'radio' || type == 'checkbox') {
                var id = $(this).attr('id');
                checked = '';

                if ($(this).attr('checked')) {
                    checked = 'checked';
                }

                //Add the pretty element and hide the original.
                $(this).before('<span id="pretty_'+ id +'" class="'+ type +' '+ checked +'"></span>');
                $(this).css({ display: 'none' });

                //Click event for the pretty input and associated label.
                $('#pretty_'+ id +', label[for='+ id +']').click(function() {
                    if (type == 'radio') {
                        //Radio must uncheck all related radio inputs.
                        $(this).siblings('span.radio.checked').removeClass('checked');
                        $(this).siblings('input:radio:checked').removeAttr('checked');

                        //And then check itself.
                        $('#pretty_'+ id).addClass('checked');
                        $('#'+ id).attr('checked', 'checked');
                    } else if (type == 'checkbox') {
                        if ($('#'+ id).attr('checked')) {
                            //Checkbox must uncheck itself if it is checked.
                            $('#pretty_'+ id).removeClass('checked');
                            $('#'+ id).removeAttr('checked');
                        } else {
                            //Checkbox must check itself if it is unchecked.
                            $('#pretty_'+ id).addClass('checked');
                            $('#'+ id).attr('checked', 'checked');
                        }


                    }
                });
            } //EOF: Radios and checkboxes.


        });
    });
};

This works great for the radio, but the checkbox seems to get stuck when clicking the checkbox label for a second time – the first click of the label successfully changes it to the appropriate state, but clicking the label again doesn’t change it (however the checkbox itself still works fine). It works perfectly in IE8.

I’ve checked the id is correct and it is. I’ve also tried a few other methods I stumbled across of checking if the checkbox is checked, but they either gave the same result or failed altogether. 🙁

Any help would be much appreciated.
Thanks 🙂

Update
Here is the HTML, which is generated by a PHP class. This is after the jQuery has run and added in the span elements:

<div>
    <p class="label">Test:</p>
    <span id="pretty_test_published_field" class="checkbox"></span>
    <input class="checkbox" id="test_published_field" name="test" type="checkbox" value="published">
    <label for="test_published_field" class="radio">Published</label>
    <span id="pretty_test_draft_field" class="checkbox"></span>
    <input checked="checked" class="checkbox" id="test_draft_field" name="test" type="checkbox" value="draft">
    <label for="test_draft_field" class="radio">Draft</label>
</div>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T12:10:39+00:00Added an answer on May 15, 2026 at 12:10 pm

    Just use the infallible and extremely simple DOM checked property. jQuery is inappropriate for this and apparently makes one of the simplest JavaScript tasks there is error-prone and confusing. This also goes for the id and type properties.

    $('input', this).each(function() {
        var type = this.type;
        if (type == 'radio' || type == 'checkbox') {
            var id = this.id;
            var checked = this.checked ? 'checked' : '';
    
            // Etc.
        }
    });
    

    UPDATE

    The default action of clicking a <label> element associated with a checkbox is to toggle the checkbox’s checkedness. I haven’t tried this myself with labels for hidden form elements, but perhaps the toggling is still happening because you’re not preventing the browser’s default click action. Try the following:

    //Click event for the pretty input and associated label.
    $('#pretty_'+ id +', label[for='+ id +']').click(function(evt) {
        if (type == 'radio') {
            //Radio must uncheck all related radio inputs.
            $(this).siblings('span.radio.checked').removeClass('checked');
            $(this).siblings('input:radio:checked').each(function() {
                this.checked = false;
            });
    
            //And then check itself.
            $('#pretty_'+ id).addClass('checked');
            $('#'+ id)[0].checked = true;
    
            evt.preventDefault();
        } else if (type == 'checkbox') {
            if ($('#'+ id).attr('checked')) {
                //Checkbox must uncheck itself if it is checked.
                $('#pretty_'+ id).removeClass('checked');
                $('#'+ id)[0].checked = false;
            } else {
                //Checkbox must check itself if it is unchecked.
                $('#pretty_'+ id).addClass('checked');
                $('#'+ id)[0].checked = true;
            }
    
            evt.preventDefault();
        }
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 440k
  • Answers 440k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Yes, I believe you're on the right lines. See my… May 15, 2026 at 5:19 pm
  • Editorial Team
    Editorial Team added an answer SELECT T1.RequestNo , T1.Type , T1.Status, T2.ActionId , T2.LastUpdated FROM… May 15, 2026 at 5:19 pm
  • Editorial Team
    Editorial Team added an answer For update statements, you should use the SqlCommand object. SqlCommand… May 15, 2026 at 5:19 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.