0

I have some HTML resembling this:

<input name="arr[something completely random][something else I can't rely on][initial]" />
<!-- ... -->
<input name="arr[dadedadeda][blahblahblah][initial]" />

And my JavaScript document.querySelectorAll('[name~="initial"]') returns [] (empty). Why? Are multidimensional arrays in names dealt with differently? Is there something I'm missing?

I just tried document.querySelectorAll('[name$="[initial]"]'), the ends with selector (attr$="endStr"), and this returns what I need, but I still need to know why the attribute contains selector (attr~="inStr") is failing to work

Isaac
  • 11,409
  • 5
  • 33
  • 45
  • I don't think this is a duplicate, the other question may have the same answer as this question, but it is not the same question – Isaac Aug 04 '15 at 06:05

1 Answers1

1

For the word contains filter, the word should be separated by spaces, in your case that fails since you have the word enclosed in []

instead you can use contains selector like

document.querySelectorAll('[name*="initial"]')
document.querySelectorAll('[name*="[initial]"]')//to be more specific
Arun P Johny
  • 384,651
  • 66
  • 527
  • 531
  • Thank you for this, didn't realise there was a difference between contains and... **really** contains – Isaac Aug 04 '15 at 06:04
  • @Isaac: Think of it as the difference between searching a list of words, and searching a string for a substring. ~= is the former, *= is the latter. – BoltClock Aug 04 '15 at 11:03
  • 1
    @BoltClock I ended up sticking with the `ends with` selector, just to eliminate the possibility of `[initial]` being one of the first two keys – Isaac Aug 04 '15 at 11:13