2

So I want to change a hidden form field's value from False to True when the user enters input data in another form field. This works fine with my select fields (dropdowns) in my form, but for my checkboxes and text input fields it isn't working. This is my code

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

So when they change the vmake field (a dropdown menu), the carsearch field changes its value to True. This works fine so I figured that if that worked, I could do the same thing with any text input, or checkbox, but it doesn't seem to work. Is the .change event not appropriate to define a text input or a checkbox? Should I use a different event handler? This is an example of me trying to do the same thing with a checkbox field, but it doesn't work.

$(function(){
    $('#year[]').change(function(){
    $('#carsearch').val('True');
});
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Rookieatthis
  • 167
  • 3
  • 11
  • take a look here http://stackoverflow.com/questions/426258/how-do-i-check-a-checkbox-with-jquery-or-javascript – t q Mar 11 '12 at 07:12

2 Answers2

2

It looks like you're using the wrong selector and event for the checking of a check box. Use the click event to see changes in the checked state of a check box

$('#year').click(function() {
  var checked = $(this).is(':checked');
  var value = checked ? 'True' : 'False';
  $('#carsearch').val(value);
});
JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
  • I was looking for the .click, how I didnt see it I dont know. Thanks for your help for a poor old beginner like me :) – Rookieatthis Mar 11 '12 at 07:36
0

IS your CheckBox's ID really "year[]"? That doesn't seem like a good choice for an ID. Remember, you should always have unique ID's.

DanRedux
  • 9,119
  • 6
  • 23
  • 41