After adding the checked attribute to the input tag through JS, and then calling the removeAttribute () method, why can't you delete it?

removeAttribute () I know I can delete the attribute on the tag, but after adding the attribute of checked to the input tag through JavaScript, then call the removeAttribute () method, why can"t I delete it? Is there a big boss to help explain?

<input type="checkbox" id="input">removeAttribute()
    <script>
        var input = document.getElementById("input");
        input.checked=true;
        input.removeAttribute("checked");
        //checked(),domchecked
        input.class="demo";
        input.removeAttribute("class");
        //class
    </script>

clipboard.png

Mar.17,2021

first answer: .prop ("checked", false)

if you are interested, you can see why?

ide/3.0/-sharpbreaking-change-removeattr-no-longer-sets-properties-to-false" rel=" nofollow noreferrer "> https://jquery.com/upgrade-gu.

Prior to jQuery 3.0, using .removeAttr () on a boolean attribute such as checked, selected, or readonly would also set the corresponding named property to false. This behavior was required for ancient versions of Internet Explorer but is not correct for modern browsers because the attribute represents the initial value and the property represents the current (dynamic) value.

It is almost always a mistake to use .removeAttr ("checked") on a DOM element. The only time it might be useful is if the DOM is later going to be serialized back to an HTML string. In all other cases, .prop ("checked", false) should be used instead.


use input.removeAttribute ("checked"); use input.setAttribute ("checked", "checked"); set the property checked, and then remove, can be removed successfully, although long ago, I hope it will be helpful.

Menu