Mohan Ram
Mohan Ram

Reputation: 8463

Check box creation doesn't work in ie7

$("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

Answers (4)

Mohan Ram
Mohan Ram

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

Mutation Person
Mutation Person

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

joelt
joelt

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

Reto Aebersold
Reto Aebersold

Reputation: 16624

Try to set 'checked': 'checked', not true

Upvotes: 3

Related Questions