Flag creation does not work in ie7

$("div").append($(document.createElement("input")).attr({
                         'id':    'post_category',
                         'value': 'item',
                        'type':  'checkbox','checked': true
                     }));

I am trying to create a checkbox with checked but ie7 do not display anything. while other browsers display the checkbox as checked. How to display the checkbox selected in ie7

+3
source share
4 answers

Code works in another browser

$("div").append("<input id='post_category' value='item' type='checkbox' checked='checked'>");

Solution To insert a checkbox as indicated 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 will not accept a valid attribute when inserting elements. therefore, after adding a check box, select this check box and set the attribute as checked.

+2
source

For some reason you cannot do it this way:

$("div").append("<input id='post_category' value='item' type='checkbox' checked='checked'>");

You can build it dynamically, according to your original intention:

$("div").append($("<input>").attr({
                     'id':    'post_category',
                     'value': 'item',
                    'type':  'checkbox','checked': true
                 }));

JSFiddle: http://jsfiddle.net/ReErJ/2/

,

'checked': true

'checked': 'checked'
+5

Try setting 'checked': 'checked', not true

+3
source

I copied the code you posted to a new page and I get 2 checkboxes in IE7. Perhaps you have a problem somewhere else in your code?

0
source

Source: https://habr.com/ru/post/1780885/


All Articles