Reputation: 8463
$("div").append($(document.createElement("input")).attr({
'id': 'post_category',
'value': 'item',
'type': 'checkbox','checked': true
}));
I am trying to create checkbox with checked but ie7 display nothing. while other browsers displays checkbox as checked. How to display checkbox selected in ie7
Upvotes: 3
Views: 383
Reputation: 8463
Code works in other browser
$("div").append("<input id='post_category' value='item' type='checkbox' checked='checked'>");
Solution To insert checkbox as checked in ie7
var category_checkbox = $("<input>").attr({'name' : 'post_category[]' , 'id' : 'post_category' , 'type' : 'checkbox' , 'value' : '1'});
$("div").append(category_checkbox);
category_checkbox.attr({'checked' : 'checked'})
IE7 wont accept checked attribute while inserting elements. so once we append checkbox refer that checkbox and set attribute as checked.
Upvotes: 2
Reputation: 30498
Any reason you can't do it like this:
$("div").append("<input id='post_category' value='item' type='checkbox' checked='checked'>");
You could construct it dynamically, as per your original intention:
$("div").append($("<input>").attr({
'id': 'post_category',
'value': 'item',
'type': 'checkbox','checked': true
}));
As per this JSFiddle: http://jsfiddle.net/ReErJ/2/
I also notice that in your code you have
'checked': true
That should be
'checked': 'checked'
Upvotes: 4
Reputation: 2680
I copied the code you posted into a new page, and I get 2 checkboxes in IE7. Perhaps you have an issue somewhere else in your code?
Upvotes: 0