0

I am using a form field to capture stats of a particular situation. I need the value of a hidden field to change based on the input of two text fields. I will describe the function in plain English. Help translating this to a functioning jQuery script would be greatly appreciated.

Hidden field equals 'yes' where the value of field_1 equals 4 && field_2 equals 2 or Hidden field equals 'yes' where the value of field_1 equals 3 && field_2 equals 1 or Hidden field equals 'yes' where the value of field_1 equals 2 else Hidden field equals 'no'

As indicated by the structure of the statement, I'm a php developer first. It is my assumption that this can be done via jQuery. If not, provide me with an alternative. Thanks!

2
  • I suspect for some reason that you are probably not using parseInt to convert the input to int's in Javascript even then string comparison in javascript should work, either way my solution below which i quickly typed up so may not be accurate. Commented Aug 7, 2011 at 20:30
  • The method posted by Ali is recognizing the initial values (returning no since both are empty) but is not updating when the values of field1 and field2 are changed. Commented Aug 8, 2011 at 13:47

2 Answers 2

1

Hope this helps. Check out jQuery api incase your fields are different some how (like if they are checkboxes)

var field1 = parseInt($('#field_1').val());
var field2 = parseInt($('#field_2').val());

if((field1 == 4 && field2 == 2) || (field1 == 3 && field2 == 1) || field1 == 2){
   $('#hidden').val('yes')
} else {
   $('#hidden').val('no')
}
Sign up to request clarification or add additional context in comments.

2 Comments

The initial values of field1 and field2 (null) are being recognized by the script and setting the value of hidden to no. However, when the values of field1 and field2 are changed, the value of hidden is not recalculated.
If the initial value is null then before you parseInt check if the value is null or not and set it to your default value. Yes, this code does not account for values changing, you'll have to bind field_1 and field_2's onBlur onKeyUp whatever. For that you would have to $('#fieldId').bind('blur', function(){ /* above code here */}); You would have to do this for both the fields.
0

The following use the keyup event handler to modify the hidden input value based on the input value of field_1 and field_2 (assuming they are text input).

$('#field1 #field2').keyup(function() {
    var field_1 = parseInt($('#field_1').val());
    var field_2 = parseInt($('#field_2').val());

    if (field_1 == 4 && field_2 == 2) {
        $('#hidden').val('yes');
    }
    else if (field_1 == 3 && field_2 == 1) {
        $('#hidden').val('yes');
    }
    else if (field_1 == 2) {
        $('#hidden').val('yes');
    }
    else {
        $('#hidden').val('no');
    }
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.