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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Chris StanyonWebDevCommented:
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
Tom BeckCommented:
Why not just set the value in the markup?
<input type="hidden" id="recipientId" value="6" />

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

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 StanyonWebDevCommented:
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 StanyonWebDevCommented:
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 StanyonWebDevCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.