Adding Text to checkbox control in JQuery Adding Text to checkbox control in JQuery jquery jquery

Adding Text to checkbox control in JQuery


The correct markup for what you're trying to do is this:

<label><input type="checkbox" id="someID">Hello World</label>

Input elements cannot contain content (and only require a closing tag or self-closing in xhtml).

To create the above with jQuery try some variation on this:

var $ctrl = $('<label />').html('Hello world')                          .prepend($('<input/>').attr({ type: 'checkbox', id: 'someID'}));

Demo: http://jsfiddle.net/bwxza/


try this

 var $ctrl =  $(document.createElement("input")).attr({                     id:    'topicFilter-'                    ,name:  'test'                    ,value: 'test'                    ,text :'my testing'                    ,type:  'checkbox'                    ,checked:true            })var lbl =  '<label>Hello world</label>'; $("#renderedControl").append($ctrl.after(lbl));

Hops its helps


first if you want to create the check box do it like this

$("#renderedControl").append('<input type="checkbox" id="someID"/>');

Notices that html of the check should be define like this

<input type="checkbox" name="example" id="someID" /> 

If you want to set a label you have to create a element for that checkbox like this, the reason behind that you cannot see the Hellow world it's not correct of use text to set a label for a checkbox and I think it's not even a valid attribute for that type. Here's an example of the html related to a label for that checkbox

<label for="example">Hellow world</label>

And then append all the html related like this

$("#renderedControl").append('<label for="checkbox">Hellow world</label>').append('<input type="checkbox" name="checkbox" id="someID" />')

At the end you could create something like this

http://jsfiddle.net/ynMK8/