0

I have a NodeList of Input (textbox) elements called "selectedRowFields". I can select the specific element I want by index using selectedRowFields.item(index). When I select one of the Input elements from selectedRowFields, and then use the textContent property to try to get the text of that Input element, it's returning blank.

Here's my code example:

var selectedRowFields = document.querySelectorAll("input.MySpecificClass");

var description = selectedRowFields.item(0).textContent;

alert(description);

Here is the alert window I'm getting (in Chrome, running locally):

Alert Window

I've verified the NodeList is not empty. It is the size I expect based on my querySelectAll() call. But every property I've tried to get the text of my Input elements is either null or blank. (E.g. nodeValue is null when I try it.)

J.D.
  • 954
  • 6
  • 22
  • 4
    `` elements cannot have *any* content. You're probably interested in the "value" property. – Pointy Jan 07 '20 at 15:20
  • "value" is not a property of node though, but "nodeValue" is, although "nodeValue" is also null when I tried it. – J.D. Jan 07 '20 at 15:23
  • 3
    a nodeList is essentially an array, have you tried `selectedRowFields[0].value` instead ? – Nicolas Jan 07 '20 at 15:25
  • `selectedRowFields` is an array, because you used `querySelectorAll`, which returns many instances (or, even just one instance) in form of an array. As opposed to `querySelector` which looks for a unique selector (and returns only one instance not in form of array). – GrafiCode Jan 07 '20 at 15:25
  • 1
    Hmm interesting. @Nicolas suggestion worked. I read querySelectorAll returns NodeList but anyway I'm good now. Thank you. – J.D. Jan 07 '20 at 15:28
  • 1
    It does return a NodeList, but JS does automatic type conversions. When you hit the .value property, JS says "Oh, hey, this Node is actually an input field, and does indeed have that property." – Nathan Miller Jan 07 '20 at 16:11
  • Ah ok, that is rough on Visual Studio's intellisense I guess then since it only shows the current object's properties and methods, but it makes sense to me. Thanks! – J.D. Jan 07 '20 at 17:18
  • 1
    Yeah, that's why I thought I'd toss that out there. JS has types only in the loosest use of the term, which can be jarring for someone used to strongly-typed languages. Sometimes I wish C# did the same. So many un-needed casts to List... – Nathan Miller Jan 07 '20 at 17:24
  • I like my strongly typed languages like C#, but I see your perspective in this case. :) – J.D. Jan 07 '20 at 17:31

0 Answers0