Rookieatthis
Rookieatthis

Reputation: 157

Jquery that changes hidden input value when another field is altered and how to reverse it?

I am using Jquery to change the value of a hidden input to 'True' when the user inputs a value into a different form field. It looks like this

$(function(){
$('#vmake').change(function(){
$('#carsearch').val('True');
});

It works great except if the user changes their mind and deletes their input, the hidden input keeps the value of 'True'. I want to make it so that if they change their mind and delete the #vmake input, it changes the value of #carsearch back to 'False'.

I would really appreciate any input on how this is accomplished. Thanks in advance!

Upvotes: 0

Views: 1030

Answers (1)

Paul Alexander
Paul Alexander

Reputation: 32377

$(function(){
$('#vmake').change(function(){
  $('#carsearch').val($(this).val() != '' ? 'true' : '');
});

This sets the value to 'true' if #vmake has a value, or '' if it doesn't.

Upvotes: 4

Related Questions