jquery selector can't read from hidden field

Andy picture Andy · Jun 15, 2009 · Viewed 73.8k times · Source

(answers aggregated into another question)

The following jquery 1.3.2 code works:

<input type="select" value="236434" id="ixd" name='ixd' />

<script>
console.log( $('#ixd') );
console.log( $("input[name='ixd']") );
</script>

Console shows:

[input#ixd 236434]

[input#ixd 236434]

However setting the input to "hidden" prevents the selectors working. Any clues?

<input type="hidden" value="236434" id="ixd" name='ixd' />

<script>
console.log( $('#ixd') );
console.log( $("input[name='ixd']") );
</script>

Console shows:

[]

[]

Answer

Mike Trpcic picture Mike Trpcic · Jun 15, 2009

Not sure why that would be failing. I do the same thing at work on a regular basis, and it works regardless of the formfield being hidden or not.

Perhaps try this:

<input type="hidden" value="236434" id="ixd" name='ixd' />

<script>
    console.log($("#xid").val())
</script>

That will get you the value of the hidden field. To get the value out of a form field, the .val() method needs to be used.