javascript - Empty classList returns true -



javascript - Empty classList returns true -

i wonder why next alert displays while classlist empty string:

<p id="p">hello, world!</p> <button id="b">button</button> <script> document.getelementbyid('b').onclick = function () { if (document.getelementbyid('p').classlist) { alert('full!'); } }; </script>

demo

because classlist object of type domtokenlist overridden methods tostring() (that's why alert(document.getelementbyid('p').classlist) shows empty string) etc.

so when execute if (document.getelementbyid('p').classlist) { have smth if (object) { , in case if pass.

to ensure - see typeof(document.getelementbyid('p').classlist) - collection. seek write in console document.getelementbyid('p').classlist.__proto__ - you'll see domtokenlist-prototype object has link domtokenlist contructor (document.getelementbyid('p').classlist.__proto__.constructor).

read more domtokenlist - https://developer.mozilla.org/en/docs/web/api/domtokenlist

javascript html boolean

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -