Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery selector can't read from hidden field

(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:

[]

[]

like image 450
Andy Avatar asked Jun 15 '09 15:06

Andy


People also ask

How to detect value change on hidden input field in jQuery?

There two possible way to detect the value change on the hidden input field: By using bind() method. By using change() method.

Can JavaScript read hidden field value?

In JavaScript, you can use following two ways to get hidden field value in a form : document. getElementById('hidden field id'). value.

How do you get hidden inputs?

The <input type="hidden"> defines a hidden input field. A hidden field lets web developers include data that cannot be seen or modified by users when a form is submitted.

Can jQuery find hidden elements?

You can simply use the jQuery :visible or :hidden selector to select all the visible or hidden elements in an HTML page. The jQuery :visible selector considered an element visible if they consume space in the document.


2 Answers

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.

like image 93
Mike Trpcic Avatar answered Sep 19 '22 07:09

Mike Trpcic


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

Is that even valid markup?

It appears that selecting a visible input retrieves the value of it, even without explicity calling .val(), whereas selecting a hidden one does not:

Try:

console.log( $('#ixd').val() );
console.log( $("input[name='ixd'][type='hidden']") );

and

console.log( $("input[name='ixd']").val() );
like image 23
karim79 Avatar answered Sep 23 '22 07:09

karim79