Choosing a dynamic option using jquery

JSFiddle .

Rules:

  • If the ".adminBox" checkbox is checked, you must select input[value='admin'].
  • If not, it input[value='sei']should be.

I tried this one but it did not work.

Any brilliant idea please?

+3
source share
2 answers

Few observations

  • You need to listen to input using the class adminBox, not to all the checkboxes
  • Use the checked property to check if it is checked.
  • adminBox is not a class name
  • use .val () to set the value of the select element

It should be

$("input.adminBox").change(function () {
    if (this.checked){
        $("#role").val('admin');
    } else {
        $("#role").val('sei');
    }
});

Demo: Fiddle

+2
source
$("input[type='checkbox']").change(function () {
        var that = $(this);
        if(this.checked)
        {
            if(that.hasClass('adminBox'))
            {
               $('#role').val('admin');
            }

            else
            {
                 $('#role').val('sei');
            }
        }

        else
        {
             $('#role').val('sei');
        }
    });
0
source

All Articles