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

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

smfmetro10Asked:
Who is Participating?
 
Tom BeckCommented:
Why not just set the value in the markup?
<input type="hidden" id="recipientId" value="6" />

Open in new window

0
 
Chris StanyonCommented:
Almost:

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

Open in new window

0
 
Rainer JeschorCommented:
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
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

 
Duy PhamFreelance IT ConsultantCommented:
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

0
 
smfmetro10Author Commented:
HA!! Brilliant
0
 
Chris StanyonCommented:
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 !!
0
 
smfmetro10Author 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
0
 
Chris StanyonCommented:
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.
0
 
Tom BeckCommented:
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.
0
 
Chris StanyonCommented:
Tom - We've obviously read it differently:

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

:)
0
 
Duy PhamFreelance IT ConsultantCommented:
@smfmetro10:  
Sometimes the simplest answer is the best solution
Couldn't agree with you more :-).
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.