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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T07:02:59+00:00 2026-06-18T07:02:59+00:00

I am trying to use jquery to: 1- dynamically add a checkbox 2- position

  • 0

I am trying to use jquery to:

1- dynamically add a checkbox
2- position it correctly.

So far I can do 1 or 2 but not 1 and 2.

Here is my HTML code:

<!-- language: lang-html -->
<body>   
 <div id='myDiv'></div>
 <div id='myOtherDiv'>
 <button id='myButton'>my button</button>
 </div> 
</body>

and here is my js code:

<!-- language: lang-js -->
jQuery(document).ready(

function ($)
{
  jQuery.noConflict();

function createCheckbox()
{
var   $myCheckbox   = $('<input id="myCheckbox" type="checkbox"></input><label for="myCheckbox">my checkbox</label>');
 $('#myDiv').append($myCheckbox);

 /*                                                                                                                                                          
  The following line fixes the "button displayed as unpressed" problem                                                                          
  but it breaks the .position call                                                                                                                           
 */
 // $myCheckbox = $('#myDiv > input');                                                                                                                       

    /* myCheckbox */
   $myCheckbox.button({
                      text: false,
                      icons: { primary: "ui-icon-refresh"}
      });

  $myCheckbox.change(
      function()
              {
                  if($myCheckbox.is(":checked"))
                  { alert('checked'); }
        else
                  {   alert('unchecked');}
              }
    );

var $myButton = $('#myButton');
$myCheckbox.position({my:"left", at:"right", of:$myButton });
}

createCheckbox();

});

The code above works but after clicking on the checkbox and clicking “OK” on the “checked” alert, the jquery checkbox button is displayed as unchecked even though the actual checkbox is checked.

By trial and error I fixed that problem by reselecting my checkbox after append. I am not sure why that works though.

$myCheckbox = $('#myDiv > input');

When I uncommenting the line above, then the checkbox behaves properly and is displayed as checked when it is checked. But then the call to .position does not position the checkbox button properly.

see: http://jsfiddle.net/roumbaba/uM3sS/1/

note: This problems only occurs when i dynamically add an input/checkbox. If I only add a simple button (using a plain instead of then both position and checked display satus work fine.

I am clearly missing something major here in how the whole process works. In particular I do not understand why reselecting my checkbox with the line

 $myCheckbox = $('#myDiv > input');

changes the behavior of the check status button. And why does position not work after I do that.

  • 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-18T07:03:01+00:00Added an answer on June 18, 2026 at 7:03 am

    The position problem was that I used .position on the checkbox but should have used it on the checkbox’s label instead. Apparently jqueryui uses the checkbox’s label and not the checkbox itself to display it’s button.

    <!-- language: lang-js --> 
    var $myCheckboxLabel = $("label[for='myCheckbox']");
    $myCheckboxLabel.position({my:"left", at:"right", of:$myButton });
    

    this fixes the position problem while still keeping the checkbox status properly working.

    I am still unclear as to why I have to first append my checkbox to the dom and then select it again in order to have a properly functioning checkbox/button after calling .button though.

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

Sidebar

Related Questions

I am trying to use Javascript / JQuery to dynamically create several HTML buttons,
I am trying to use jQuery Tools to setup some tooltips dynamically, but I
I am need paint my image. I'm trying use JQuery in here this link:
I'm trying to figure out how to use JavaScript and jQuery to dynamically create
I have been trying to use a simple jQuery operation to dynamically match and
Hello I'm trying to dynamically generate some inputs for my form, but it's not
I am trying to add the remove buttons dynamically from a jQuery UI Dialog,
I have the following bit of code. I'm trying to dynamically add drop-down options
I'm trying to use the jQuery forms plugin to dynamically submit a form on
I'm trying to use the jQuery-File-Upload plugin . But I have a problem when

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.