Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IE7 / IE8 interaction with JQuery .removeAttr('disabled'), not applying css

This is more of a curiosity than a question, but I'm wondering why the following isn't working.

Say I have an html form with a selector and a submit button.

 <form action="/foo/createActivity" method="post" name="activityform" id="activityform" >

<select name="activity" id="activity" >
<option value="1" >Activity 1</option>
<option value="2" >Activity 2</option>
</select>
<input type="submit" class="submit" id="add" value="Add >>"/>
</form>

Then I have an ajax call with jQuery defined

$('#activity').change(function() {
    tryDisable($('#activity').val());
});

function tryDisable(activity) {
$.ajax({
    type: 'GET',
    contentType: 'application/json',
    cache: false,
    url: '/foo/checkActivity',
    data: {
        activity: activity
    },
    success: function(disableSubmit) {

        if (disableSubmit == "true") {
            $('#add').attr('disabled', 'true');
        } else {
            $('#add').removeAttr('disabled'); // Problem in IE, doesn't take away CSS of disabled
            // $('#add').css({"text-align" : "center"}); 
            // NO idea why the above fixes the problem, TODO find out
        }
    },
    dataType: 'json'
});
return false;
}

You'll see in my comments that just the .removeAttr('disabled') is causing the button to re-enable, but the button still looks like it's disabled (gray-ed out). However, if I 'tickle' the css via jquery as I did in the commented out line, the correct, non-disabled styling applies. Firefox and Chrome work fine with just the first line, as I expected.

Does anyone know why this might be? What's IE doing here that's so odd?

like image 243
Will Buck Avatar asked Jan 18 '23 13:01

Will Buck


1 Answers

I did have the same problem before. I googled it and found this note from jQuery website.

Note: Trying to remove an inline onclick event handler using .removeAttr() won't achieve the desired effect in Internet Explorer 6, 7, or 8. To avoid potential problems, use .prop() instead:

$element.prop("onclick", null);
console.log("onclick property: ", $element[0].onclick); // --> null

I also found a couple of posts here in SO regarding the .removeAttr():

  • removeAttr() issue
  • .prop('checked',false) or .removeAttr('checked')?

This post by the John Resig is also helpful.

like image 141
Annie Lagang Avatar answered Mar 02 '23 00:03

Annie Lagang