I'm creating some checkbox elements on the fly with jQuery and appending them to a node like so
var topics = ['All','Cat1','Cat2'];
var topicContainer = $('ul#someElementId');
$.each( topics, function( iteration, item )
{
topicContainer.append(
$(document.createElement("li"))
.append(
$(document.createElement("input")).attr({
id: 'topicFilter-' + item
,name: item
,value: item
,type: 'checkbox'
,checked:true
})
.click( function( event )
{
var cbox = $(this)[0];
alert( cbox.value );
} )
)
.append(
$(document.createElement('label')).attr({
'for': 'topicFilter' + '-' + item
})
.text( item )
)
)
} );
The problems I'm encountering are two-fold (there are in IE only)
- Checkboxes are added to the page, but their default checked state is unchecked, when I'm specifying 'true' for that value. (Testing with 'checked' for the value makes no difference)
- When
alert( cbox.value );
executes, the output is 'on', every time.
I think the core problem here is I need a better way to set the default checked state of the checkboxes, and to set their default "value" attribute. But I haven't yet found another way.
Note: all of this code works fine in Firefox and Chrome.
This is jQuery 1.3.1 testing with IE 7.0.5730.11