0
Can anybody explain me how textarea length is zero ( 0 ).... I am beginner need help.
divElem. addEventListner('click', function () { Let noTextarea = document. getElementByClassName( 'textarea'). length; If(noTextarea == 0) { let html = divElem. innerText; divELem. innerHTML = '<textarea class="form-control textarea " Id="textarea " rows='8'>${html}</textarea>'; } }) ;
3 Answers
+ 1
The `document.getElementsByClassName` method returns a NodeList, which shares some similarity to an array, in which you can iterate through it, and has `length` property which returns the count of items are within it.
The method is used to collect all elements whose `class` attribute value matches to the method's given argument.
I think you were looking for `document.getElementsByTagName` instead. This method collects all elements whose tag name matches to the method's given argument In your case, on success this method will return a NodeList containing all the textarea elements in your document. But if there was no textarea found, then of course the returned NodeList is empty, which also means, the NodeList.length property value is zero, because it contains no element.
https://www.w3schools.com/jsref/met_document_getelementsbytagname.asp
P.S. You wrote the method name wrong. It should be
getElementsByClassName
Instead of
getElementByClassName
I know it's a minor typo, but it's a problem nonetheless.
+ 1
Thanks a lot.
0
You're welcome đ