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 8437057
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T07:19:56+00:00 2026-06-10T07:19:56+00:00

(jQuery noob here) Im trying to write a script which when I write <input

  • 0

(jQuery noob here)

Im trying to write a script which when I write <input type='checkbox'/> will automatically convert it to jQuery UI button and look like a checkBox.
Sample code so far …

var newCheckboxID = 0;
$( "input:checkbox" ).attr('id', "cbx-" + nextCheckboxID++); // how to do that?
$( "input:checkbox" ).after("<label style='width:16px; height:16px; vertical-align:middle;'></label>");
$( "input:checkbox" ).next().attr("for", $(this).attr('id') ); // doesn't work for sure
$( "input:checkbox" ).button();
$( "input:checkbox" ).button( "option", "text", false );
$( "input:checkbox" ).attr("onclick", "$(this).button( 'option', 'icons', {primary:((this.checked)?'ui-icon-check':null),secondary:null} )");
        

Sorry, if it’s too obvious but I’ve lost more than hour in that …

EDIT

Finally did it with the old fashioned way (for the doesn’t working parts).
Any comments for making it more compact and "more jQuery" would be appriciated …
Code sample

// ---- set ids
var checkboxID = 0;
//$( "input:checkbox" ).attr('id', "cbx-" + nextCheckboxID++); // how to do that?
var cboxes = document.getElementsByTagName('input');           // <-- do this instead
for(var i=0; i<cboxes.length; i++){
    if( cboxes[i].getAttribute('type')!='checkbox' ) continue;
    cboxes[i].setAttribute('id', 'cbx-'+checkboxID++);}

// ---- add labels
$( "input:checkbox" ).after("<label style='width:16px; height:16px; vertical-align:middle;'></label>");
//$( "input:checkbox" ).next().attr("for", $(this).attr('id') ); // doesn't work this
for(var i=0; i<cboxes.length; i++){                              // <-- do this instead
    if( cboxes[i].getAttribute('type')!='checkbox' ) continue;
    cboxes[i].nextSibling.setAttribute('for', cboxes[i].getAttribute('id') );}

// ---- create
$( "input:checkbox" ).button();
$( "input:checkbox" ).button( "option", "text", false );
$( "input:checkbox" ).attr("onclick", "$(this).button( 'option', 'icons', {primary:((this.checked)?'ui-icon-check':null),secondary:null} )");
  • 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-06-10T07:19:57+00:00Added an answer on June 10, 2026 at 7:19 am

    Working examples:

    • jsFiddle (without comments)
    • jsFiddle (without comments with UI theme switcher!)
    • jsFiddle (with comments)
    • jsFiddle (Just for fun, uses timer and some other jQuery features, just for future reference)
    • jsFiddle (Just for fun, uses timer and changes UI theme every second!)

    In the following, I should note 2 primary changes. I added CSS to do what you were trying to do to labels in “code” (where it really doesn’t belong).

    Also, I changed the HTML for “ease of jQuery” use. However, I still noted in the comments how you can easily change it back.

    the HTML

    <center>
        <button>Create New CheckBox</button>
    </center>
    <hr />
    <div id="CheckBoxes">
        <input class="inp-checkbox" />
        <input class="inp-checkbox" />
        <input class="inp-checkbox" />
        <input class="inp-checkbox" />
    </div>​
    

    the CSS

    .inp-checkbox+label {
        width:16px; 
        height:16px; 
        vertical-align:middle;
    }​
    

    the JavaScript/jQuery

    // keep in mind, and i will explain, some of these "moving-parts" or not needed, but are added to show you the "ease" of jquery and help you see the solution
    
    //  This global function is designed simply to allow the creation of new checkboxes as you specified, however, if you won't be making check boxes at end user time, then i suggest simply moving it to within the .each statement found later on.
    //  Also, this could easily be written as a jQuery plugin so that you could make a "chainable" one-line call to change checkboxes to this but let's get to the nitty gritty first
    function createCheckBox(ele, i) {
        //  First I simply create the new ID here, of course you can do this inline, but this gives us a bottleneck for possible errors
        var newID = "cbx-"+i;
        //  below we use the param "ele" wich will be a jQuery Element object like $("#eleID")
        //  This gives us the "chainability" we want so we don't need to waste time writing more lines to recall our element
        //  You will also notice, the first thing i do is asign the "attribute" ID
        ele.attr({ "id": newID  })
            //  Here we see "chainability at work, by not closing the last line, we can move right on to the next bit of code to apply to our element
            //  In this case, I'm changing a "property", keep in mind this is kinda new to jQuery,
            //  In older versions, you would have used .attr but now jQuery distinguishes between "attributes" and "properties" on elements (note we are using "edge", aka. the latest jQuery version
            .prop({ "type": "checkbox" })
            //  .after allows us to add an element after, but maintain our chainability so that we can continue to work on the input
            // here of course, I create a NEW label and then immidiatly add its "for" attribute to relate to our input ID
            .after($("<label />").attr({ for: newID  }))
            //  I should note, by changing your CSS and/or changing input to <button>, you can ELIMINATE the previous step all together
            // Now that the new label is added, lets set our input to be a button,
            .button({ text: false }) // of course, icon only
            //  finally, let's add that click function and move on!
            //  again, notice jQuery's chainability allows us no need to recall our element
            .click(function(e) {
                //  FYI, there are about a dozen ways to achieve this, but for now, I'll stick with your example as it's not far from correct
                var toConsole = $(this).button("option", {
                    icons: {
                        primary: $(this)[0].checked ? "ui-icon-check" : ""
                    }
                });
                console.log(toConsole, toConsole[0].checked);
            });
        //  Finally, for sake of consoling this new button creation and showing you how it works, I'll return our ORIGINAL (yet now changed) element
        return ele;
    }
    
    $(function() {
        //  This .each call upon the inputs containing the class I asiged them in the html,
        //  Allows an easy way to edit each input and maintain a counter variable
        //  Thus the "i" parameter
        //  You could also use your ORIGINAL HTML, just change $(".inp-checkbox") to $("input:[type='checkbox']") or even $("input:checkbox")
        $(".inp-checkbox").each(function(i) {
            // as previously noted, we asign this function to a variable in order to get the return and console log it for your future vision!
            var newCheckBox = createCheckBox($(this), i);
            console.log(newCheckBox);
        });
    
        //  This next button added is simply to show how you can add new buttons at end-time
        //  ENJOY!!!
        $("button").button().on("click", function(e) {
            var checkBoxCount = $("#CheckBoxes .inp-checkbox").length;
            var newCheckBox = $("<input />").addClass("inp-checkbox").appendTo($("#CheckBoxes"));
            createCheckBox(newCheckBox , checkBoxCount);
            console.log(newCheckBox);
        });
    });​
    

    Update: The original intent here was to purely answer the question, which was to create a jQuery UI styled checkbox and show how jQuery can be used in multiple ways. However, a later comment queried how to include a traditional style label with it. While there are a billion options for this, I’ll simply take from the above and extend.

    The first option I took is pretty simple. Using jsFiddle (without comments with UI theme switcher!), I made the following changes:

    the JavaScript/jQuery

    // First I add a new variable.
    // It will simply be for a new "wrapper" element, in which to ensure our button is encased. 
    // Giving this element a new class gives easy access for later CSS or Event Triggering of sub elements (like the checkbox)
    var newID = "cbx-"+i,
        wrapper = $('<div />', { 'class': 'ui-checkbox-wrapper' }).appendTo('#CheckBoxes');
    //  Then I added a line to our ele series of methods.
    //  This line simply append our element (checkbox) to our new wrapper
    // Take Note at how I added this method at start rather than at end.
    // Had I not done this, then the label would not have been wrapped!
    ele.appendTo(wrapper)   //  <-- new line!
        .attr({ "id": newID  })
    

    Then I simply added the following CSS:

    #CheckBoxes .ui-button .ui-button-text {
        background: #A9A9A9;
        display: inline;
        font-size: 16px;
        left: 19px;
        padding: 0;
        position: relative;
        text-indent: 0;
        top: -4px;
    }
    

    Results!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

javascript noob trying to figure something out. I saw a script located here: Using
javascript noob here. So I'm trying to use this s3 uploader jQuery example here
I am a jQuery noob and seems I always will be, been trying to
Greetings from some noob trying to learn JQuery, I am attempting to make it
I'm a noob at json (know a bit of jquery)....and trying to get a
Im a jquery noob. I needed a script that would swap out images or
jQuery noob here (don't most posts start like that?) I know I could do
I'm a total noob when it comes to jQuery but I am trying to
jQuery noob here. I have a jQuery handler that makes use of $(this) .
Javascript/JQuery noob here, so apologies. I'm using .ajax to get a JSON object then

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.