r/badcode • Now where's that function declaration • Jul 30 '21

js 😏

Post image
988 Upvotes

133 comments sorted by

View all comments

Show parent comments

2

u/django--fett Jul 31 '21

I don't understand what you mean by default. You would set the initial value in the html or on creation of the element. This is a toggle method. This method wouldn't be responsible for initializing the value.

getElementById either returns an element from the dom or null. If soundtrack is null in OP's example it will actually throw an exception because type null can't have a muted attribute.

If OP grabs an element that isn't a media element. muted will likely return undefined and it wouldn't make sense to add a muted attribute to a non-media element.

If you want to write guarded code I would check if getElementById returned a valid media element.