Jan. 29, 2025, 12:38 p.m.
IT | Rants

JavaScript: prop vs attr

I just ran into a stupid issue that took 30 minutes of debugging to figure out. I have some JavaScript that would cause a radio button not to be checked by my code after a certain code flow was taken. So take code path 2 - it works, radio button gets checked. Take code flow 1 then code path 2 - does not work, radio button does not get checked.

Turns out code path 1 used prop('checked', false) and code path 2 used attr('checked', true) and they are incompatible. Each on their own worked, but if I ever set the checked property using prop, setting that same attribute using attr would no longer function. Weird.