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.
You can use wild card ends with using attribute selector, instead of using id selector.
$('[id$=rooms]')
OR, use *
for contains
$('[id*=rooms]')
Or if you have single element with id, live demo
$('#2-rooms')
Edit If you have single element with id then you can simply use id selector $('#2-rooms')
If more then one elements have same id then it is not valid html you should have unique ids. If you have ids with pattern like 2-rooms, 3-rooms then you have use attribute selector with wild cards.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With