3

I have the following code:

$("#solDate").change(function () {
    if ($(this).val().trim() == '') {
        $("#solvedBy").val('Pick an option');
     }
     else {
         $("#solvedBy").val('Please, pick the issue solver');
     }
});

$("#solvedBy").change(function () {
    if ($(this).val() == 'Please, pick the issue solver') {
    //if (document.getElementById("solvedBy").valueOf == 'Please, pick the issue solver') {
        $("#saveBtn").attr('disabled', true);
        $("#slvByValMsg").text('You have solution date and no solver');
    }
    else {
        $("#saveBtn").attr('disabled', false);
        $("#slvByValMsg").text('');;
    }
});

solDate is a date textbox, while solvedBy is a dropdownmenu. Both of these scripts work perfectly, but when I try to trigger the second with the first, it breaks.

In short, the sequence is: when people choose a solution date, on the dropdown menu the text 'Please, pick the issue solver' appears. When this happens, button should get disabled and error msg should appear until the person picks a solver.

When I set the solvedBy to 'Please, pick the issue solver', the command works. When I set the date, the value of solvedBy changes, but the second script doesn't execute (I think it doesn't recognize it as a change). The commented line didn't help too.

Ashley Medway
  • 7,151
  • 7
  • 49
  • 71
GeorgiG
  • 1,018
  • 1
  • 13
  • 29

2 Answers2

1

Your code should be like

$("#solDate").change(function () {
    if ($(this).val().trim() == '') {
        $("#solvedBy").val('Pick an option');
     }
     else {
         $("#solvedBy").val('Please, pick the issue solver');
     }
     //do change event of solvedBy here will solve issue
     $("#solvedBy").change();
});

$("#solvedBy").change(function () {
    if ($(this).val() == 'Please, pick the issue solver') {
    //if (document.getElementById("solvedBy").valueOf == 'Please, pick the issue solver') {
        $("#saveBtn").attr('disabled', true);
        $("#slvByValMsg").text('You have solution date and no solver');
    }
    else {
        $("#saveBtn").attr('disabled', false);
        $("#slvByValMsg").text('');;
    }

});
Parth Trivedi
  • 3,802
  • 1
  • 20
  • 40
1

You simply need to call the .change() method.

Note: Changing the value of an input element using JavaScript, using .val() for example, won't fire the event.

See this answer for why .val() does not automatically call .change().

$("#solDate").change(function () {
    if ($(this).val().trim() == '') {
        $("#solvedBy").val('Pick an option').change();
     }
     else {
         $("#solvedBy").val('Please, pick the issue solver').change();
     }
});
Community
  • 1
  • 1
Ashley Medway
  • 7,151
  • 7
  • 49
  • 71