parsilinux
parsilinux

Reputation: 1

Change checkbox status based on textbox value

I have a form in my site & there is a Read-Only textbox in my form. This textbox will dynamically get its value (A, B, C or D) from URL. then i have some checkboxes below this textbox. what i want is to change the status of these checkboxes based on the value of that textbox.

for example if the textbox1 value was A, then checkbox1, checkbox3 & checkbox6 become checked. I tried somthing like this but it didn't work:

$("#textbox1").change(function(){
    var txt1=$("#textbox1").val();
        if(txt1=='A'){
        $(":checkbox[name='checkbox1']").prop('checked',true);
            $(":checkbox[name='checkbox3']").prop('checked',true);
        $(":checkbox[name='checkbox6']").prop('checked',true);
        }
        else {
        //nothing
      }
      });

thanks.

Upvotes: 0

Views: 1148

Answers (1)

MrJD
MrJD

Reputation: 1889

You need to use the change event for the textbox and then evaluate which checkbox to check. Like so:

$(function(){
    $("#myTextbox").on("change",function(){
        var $Textbox = $(this);
        var NewCheckbox;
        switch ($Textbox.val()){
            case "A":
                NewCheckbox = $("#checkboxA");
                break;
            case "B":
                NewCheckbox = $("#checkboxB");
                break;
            case "C":
                NewCheckbox = $("#checkboxC");
                break;
        }
        $("#checkboxA").attr('checked',false);
        $("#checkboxB").attr('checked',false);
        $("#checkboxC").attr('checked',false);

        NewCheckbox.attr('checked',true);
    });
});

Upvotes: 1

Related Questions