0

When Y is selected, both of the input field should display "completed",and when user select "N", both of the input field should be blank but for some reason the code fail, any idea. Many Thanks

if($('.finished1').val(''))
{
	$('.finished').val('');
	
}

else if($('.finished1').val('Y'))
{
	$('.finished').val('completed');
	
}
else{
	$('.finished').val('');
	
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>
<select  class="finished1">
<option value="" ></option>
<option value="Y" > Y</option>
<option value="N" > N</option>
</select>

<table>
<td><input type="text" class="finished"></td>
<td><input type="text" class="finished"></td>
</table>

1
  • Where is your change Function ? Commented Nov 29, 2017 at 2:15

4 Answers 4

2

Try this code JSFiddle

$('.finished1').on('change',function(){
   if($(this).val() == 'N' || $(this).val() == ''){
     $('.finished').val('');
   }else{
     $('.finished').val('completed');
   }
});
Sign up to request clarification or add additional context in comments.

1 Comment

If you use this.value instead of $(this).val() you avoid two function calls, which is both easier to read and more efficient, and if you were to invert the if test and check for 'Y' instead you could avoid having to check the field value twice.
2

You should use jquery's Change API to capture any change in the select input then you can check the selected value and respond accordingly

$('.finished1').on('change', function(){
  if($(this).val() == 'Y')
    $('.finished').val('completed');
  else
    $('.finished').val('');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>
<select  class="finished1">
<option value="" ></option>
<option value="Y" > Y</option>
<option value="N" > N</option>
</select>

<table>
<td><input type="text" class="finished"></td>
<td><input type="text" class="finished"></td>
</table>

Comments

2

You'll first need to evaluate the value of the select box through a change event handler and from there you can use switch statement to determine what to do from there.

$(function() {
  $('.finished1').on('change', function() {
    switch (this.value) {
      case 'Y':
        $('.finished').val('completed');
        break;
      case 'N':
      case: '':
        $('.finished').val('');
        break;
      default:
        //
    }
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>

<select class="finished1">
  <option value="" ></option>
  <option value="Y" > Y</option>
  <option value="N" > N</option>
</select>

<table>
  <td><input type="text" class="finished"></td>
  <td><input type="text" class="finished"></td>
</table>

4 Comments

case 'N', '':? I don't think that does what you seem to think it does. Should be a fall-through.
Good catch. Fixed.
Cool. Bonus points for saying this.value rather than $(this).val().
Hey man...whenever I have a chance to go native, I most certainly do.
1

You should use a function that will detect the change in dropdown selected item.

If its detected it will check the values again and if need chang it to what is selected.

$(function () {
    $('.finished1').change(function () {
         if($('.finished1').val(''))
         {
         $('.finished').val('');    
         }
         else if($('.finished1').val('Y'))
         {
         $('.finished').val('completed');   
         }
         else{
         $('.finished').val('');    
         }
    });
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.