0

Would like to know how to link a checkbox's checked state to a value on a textfield returned from an sql query. The textbox value is updated after a user selects a value from a select/menu on the form. The value of the select menu option is used as a filter in the query, which is working fine.

if ($('#textbox').val("TRUE")
    { $('#mycheckbox').prop('checked',true);}
else
    {
     $('#mycheckbox').prop('checked',false);
   }

The problem is that the if statement works for the previous value the user selected. So its always one click behind even though the textbox value is current. Although the value of the textbox is current on the form, when I make an alert box displaying the textbox's value, it too is behind. So the if statement is ok, its just its using the textbox's previous value and not the current one. Any way to make this current?

2 Answers2

0

Your if statment is not testing the value of #textbox it is actually setting a value.

Try changing your if statement to read:

if ($('#textbox').val() == "TRUE")

Here's another SO post that might help.

Community
  • 1
  • 1
David Tansey
  • 5,813
  • 4
  • 35
  • 51
0

First, you have syntax error in your code, assuming that is a typo.

What you are doing by$('#textbox').val("TRUE") is setting the value of textbox to TRUE.

So whenever your code block will execute you will end-up with checked check box, and textbox with TRUE value.

If your goal is to update the checkbox as per the value of text box. you can put the following one liner code instead of your four line, when you are actually setting/updating the value of text box.

$('#mycheckbox').prop('checked',$('#textbox').val() == "TRUE")

Update

As you mentioned you are setting the new value after a ajax call and didn't included your code sample, I'm guessing, your code is something like:

$.ajax({
  url: "your/url"
}).done(function(data) {
  $('#textbox').val(data)
});

If so, update your code to

$.ajax({
  url: "your/url"
}).done(function(data) {
  $('#textbox').val(data);
  $('#mycheckbox').prop('checked',$('#textbox').val() == "TRUE");
});

The response is based on guess.

xiidea
  • 3,344
  • 20
  • 24
  • It still checks the box based on a value selected previously. Its one user selection behind. – user3527363 Apr 14 '14 at 02:59
  • I asked where you are setting/updating the value for textbox? – xiidea Apr 14 '14 at 03:02
  • @user3527363: And please clear one thing, **"Its one user selection behind"** your code doesn't contain the part of your selection mechanism!! – xiidea Apr 14 '14 at 03:06
  • when a user selects a value on a select menu box the id is sent by ajax and is used as a filter on a query. The query results are json parsed and the textbox value is set in this manner. So the text boxes new value is the value pulled from the MySQL database. If you want I can show you the code, but its working fine. – user3527363 Apr 14 '14 at 03:08
  • As i stated in my answer, Then you need to put the code when you are updating the value of textbox. In your case i guessing you have called `$('#textbox').val(newValue)` or something like that in your code. put my code after that. – xiidea Apr 14 '14 at 03:29
  • YES, that did it. Thank you so much, as you may have guessed I am a noob.... Works beautifully. – user3527363 Apr 14 '14 at 03:41