We help IT Professionals succeed at work.

how do you make a default value on a hidden form field

smfmetro10
smfmetro10 asked
on
Hi,

I have a hidden field that has a name and ID of "recipientId"  

if the value is null i would like to set it to 6

This is what I have so far but it's not working.

Thanks in advance for the help!

<script>
$(document).ready(function () {	
if ($(recipientId).val(null)) {
     $("recipientId").val(6);
	
             }
});
</script> 

Open in new window

Comment
Watch Question

Most Valuable Expert 2018
Distinguished Expert 2019

Commented:
Almost:

if ( $('#recipientId').val() == "" ) $('#recipientId').val('6');

Open in new window

Most Valuable Expert 2013
Awarded 2012

Commented:
Hi,

this should work:
<script>
$(document).ready(function () {	
if ($("#recipientId").val() == null || $("recipientId").val() == "") {
     $("#recipientId").val(6);
	
             }
});
</script> 

Open in new window


As far as I know, input fields do not have a value of null, they have most likely an empty string.

HTH
Rainer
Top Expert 2015
Commented:
Why not just set the value in the markup?
<input type="hidden" id="recipientId" value="6" />

Open in new window

Duy PhamFreelance IT Consultant

Commented:
By default, hidden field has value = '' (empty string), not null. So you can't check if hidden field value is null.

Maybe you should check for empty value instead:

if ($('#recipientId').val().length == 0) {
    $('#recipientId').val(6);
}

Open in new window

Author

Commented:
HA!! Brilliant
Most Valuable Expert 2018
Distinguished Expert 2019

Commented:
What!!

That answer has nothing to do with checking if you have a null value. What do you do if you don't have a null value ??

Bad question !!

Author

Commented:
when the page loads the value was null.
Clicking on various links sets the value.

I checked if ( $('#recipientId').val() == "" ) $('#recipientId').val('6');

<script>
$(document).ready(function () {      
if ($("#recipientId").val() == null || $("recipientId").val() == "") {
     $("#recipientId").val(6);
      
             }
});
</script>

and :

if ($('#recipientId').val().length == 0) {
    $('#recipientId').val(6);
}

None of those answers seemed to work.

Sometimes the simplest answer is the best solution
Most Valuable Expert 2018
Distinguished Expert 2019

Commented:
My point was that the answer you chose simply sets an input to 6 - always! it does no checking for null, and doesn't give you the option of it being set to anything other than 6. If that's what you need, then fine, but that's not what you asked for.

Fair play to Tom for his mind-reading ability ;)

If the jQuery that 3 Experts posted isn't wqorking for you, then I'd suggest that you're doing it wrong, because they all do exactly what you asked for.
Top Expert 2015

Commented:
No mind reading ability here. The text of the question may have mislead but the question was clear.
how do you make a default value on a hidden form field
The field can still be set to something else through jQuery or javascript.
Most Valuable Expert 2018
Distinguished Expert 2019

Commented:
Tom - We've obviously read it differently:

if the value is null i would like to set it to 6

:)
Duy PhamFreelance IT Consultant

Commented:
@smfmetro10:  
Sometimes the simplest answer is the best solution
Couldn't agree with you more :-).