For some reason I am unable to retrieve hidden inputs by id using jQuery.
I can do
> $('input')
[<input type="checkbox" name="property-type" id checked>, <input type="checkbox" name="property-type" id checked>, <input type="checkbox" name="property-type" id>, <input type="checkbox" name="property-type" id>, <input type="hidden" name="no-of-rooms" id="1-rooms">, <input type="hidden" name="no-of-rooms" id="2-rooms" checked>, <input type="hidden" name="no-of-rooms" id="3-rooms" checked>, <input type="hidden" name="no-of-rooms" id="4-rooms">, <input type="hidden" name="no-of-rooms" id="5-rooms">, <input type="hidden" name="no-of-rooms" id="over-5-rooms">, <input type="checkbox" name="property-type" id>, <input type="checkbox" name="property-type" id>, <input type="checkbox" name="property-type" id>]
which gets me all of the inputs on the page nicely, including those of type=hidden.
I can also do
> $('input[type="hidden"]')
[<input type="hidden" name="no-of-rooms" id="1-rooms">, <input type="hidden" name="no-of-rooms" id="2-rooms" checked>, <input type="hidden" name="no-of-rooms" id="3-rooms" checked>, <input type="hidden" name="no-of-rooms" id="4-rooms">, <input type="hidden" name="no-of-rooms" id="5-rooms">, <input type="hidden" name="no-of-rooms" id="over-5-rooms">]
which gets me all of my hidden fields. Note they all have an id.
For some reason trying to target those inputs by their id, either with or without the [type="hidden"]
included, will get me an empty list.
> $('input[type="hidden"]#2-rooms')
[]
> $('input#2-rooms')
[]
I have managed to find a workaround by simply not targeting them by their ID-s, but it would be useful to know why this doesn't seem possible.
EDIT
Using just $('#2-rooms')
works for me and is apparently the best approach.
However, I am still unsure as to why $('input#2-rooms')
is not working, as I had in fact included the html5 doctype (<!DOCTYPE html>
), and I understand ids beginning with numbers should in this case be okay.