Need JQuery function to set value of hidden field

I am building a form in SharePoint Designer 2007 and I need a JQuery function that will set the value of a hidden field when the value of a specific field changes. I am new to Jquery so I don't know if there is already an event that I can use (onchange).  For example:

Field has no value:

label    Field1

Table style="visibility:hidden"
Hidden Field(no value)



Field has value:

label    Field1=1

Table style="visibility:hidden"
Hidden Field=A
LVL 9
Michael CarrilloInformation Systems ManagerAsked:
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.

designatedinitializerCommented:
use this:

$("#hiddenField").val("A");

in the event handler of your choice.
For instance, if you want to do that onChange of input "Field1", do this:

$("#Field1").change(
function(){
    $("#hiddenField").val("A");
});
0
Michael CarrilloInformation Systems ManagerAuthor Commented:
How would I write this if Field1 has a value of X1 then hiddenField has a value of A Else
if Field1 has a value of X2 then hiddenField has a value of B Else Hiddenfield has value of C?
0
designatedinitializerCommented:
$("#Field1").change(
function(){
    var f=$("#Field1").val();
    if(f=="X1"){
         $("#hiddenField").val("A");
    else if(f=="X2"){
         $("#hiddenField").val("B");
    }else{
         $("#hiddenField").val("C");
    }
});

or:

$("#Field1").change(
function(){
    var f=$("#Field1").val();
    switch(f)
    {
        case "X1":
             $("#hiddenField").val("A");
             break;
        case "X2":
             $("#hiddenField").val("B");
             break;
        default:
             $("#hiddenField").val("C");
    }
});
0
Become a CompTIA Certified Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

Michael CarrilloInformation Systems ManagerAuthor Commented:
Not working yet.
Should the second to the last '}' have a semicolon?
To end the switch?
0
designatedinitializerCommented:
this will work:
<?php
print_r($_POST);
?>
<head> 
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.js"></script> 
    <script> 
        // wait for the DOM to be loaded 
        $(document).ready(function() { 
			$("#Field1").change(
			function(){
				var f=$("#Field1").val();
				switch(f)
				{
					case "X1":
						 $("#hiddenField").val("A");
						 break;
					case "X2":
						 $("#hiddenField").val("B");
						 break;
					default:
						 $("#hiddenField").val("C");
				}
			});
		});
    </script> 
</head> 
<body>
<form id="myForm" action="" method="post"> 
    <label for="Field1">Field1: <input type="text" id="Field1" name="Field1" /></label>
    <input id="hiddenField" name="hiddenField" type="hidden" value="XXX"/>
    <input type="submit" value="Submit" /> 
</form>
</body>

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
Michael CarrilloInformation Systems ManagerAuthor Commented:
Thanks!  Both work now.
The problem I ran into was with the if statement having the correct number of closing brackets.
As in

    if(f=="X1")
          {
              $("#hiddenField").val("A");
          }
    else
          {
               if(f=="X2")
                    {
                             $("#hiddenField").val("B");
                    }
                else
                    {
                             $("#hiddenField").val("C");
                    }
         }
0
Michael CarrilloInformation Systems ManagerAuthor Commented:
Thanks Again!
0
designatedinitializerCommented:
you're right, i was lacking a bracket.
"else if" is a correct construct, but there was missing a "{" before it.
This will work:
$("#Field1").change(
function(){
    var f=$("#Field1").val();
    if(f=="X1"){
         $("#hiddenField").val("A");
    }else if(f=="X2"){
         $("#hiddenField").val("B");
    }else{
         $("#hiddenField").val("C");
    }
});

Open in new window

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
jQuery

From novice to tech pro — start learning today.