Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

radio Buttons and .attr('checked','checked') does NOT work in IE7

Is there any way to get radio buttons checked upon appending in IE7?

What seems to work in every browser, doesn't look like it works in IE6,7 despite reading everywhere that I'm doing it correctly. I have absolutely no idea why it's not working.

var $itemVariantRowRadio = $("<input/>")
    .attr("type", "radio")
    .attr("name", "itemvariant")
    .addClass("itemvariant")
    .val('whatever');


    $itemVariantRowRadio.attr('checked', 'checked');
    $itemVariantRow.append($itemVariantRowRadio)

Now if I do a console.log($itemVariantRowRadio.attr('checked') in IE6/7 then it says that it's set to TRUE but the radio doesn't actually get checked or pick up as checked.

Nightmare! Anyone else come across this and have any sort of fix?

like image 938
Intellix Avatar asked Jun 21 '11 21:06

Intellix


People also ask

How do I check if a Radiobutton is enabled in Javascript?

Using Input Radio checked property: The Input Radio checked property is used to return the checked status of an Input Radio Button. Use document. getElementById('id'). checked method to check whether the element with selected id is check or not.

What is radio button in Android Studio?

Radio buttons allow the user to select one option from a set. You should use radio buttons for optional sets that are mutually exclusive if you think that the user needs to see all available options side-by-side. If it's not necessary to show all options side-by-side, use a spinner instead.


2 Answers

If in jQuery >= 1.6:

Use prop as seen here: .prop() vs .attr()

$itemVariantRowRadio.prop('checked', true);

If in jQuery < 1.6:

$itemVariantRowRadio.attr('checked', true);

ALSO:

Try creating your element like so:

var $itemVariantRowRadio = $("<input/>",{
     type: 'radio',
     name: 'itemvariant',
     class: 'itemvariant',
     checked: true,
     value: 'whatever'
});
$itemVariantRow.append($itemVariantRowRadio);

See fiddle: http://jsfiddle.net/maniator/6CDf3/
An example with 2 inputs appended: http://jsfiddle.net/maniator/6CDf3/2/

like image 188
Naftali Avatar answered Oct 16 '22 00:10

Naftali


Try: $itemVariantRowRadio.not(':checked').click().change();

So you actually click the checkbox just like you would do as a user with the mouse. not(':checked') will get you sure it was not already checked before. And trigger the change event afterwards, as jQuery click does not do that by itself.

like image 4
sod Avatar answered Oct 16 '22 00:10

sod