You are viewing limited content. For full access, please sign in.

Question

Question

If textarea is empty, set radio button to No

asked on October 17, 2018 Show version history

I am trying to set a radio button to Yes or No depending on whether or not a textarea is blank or not.

$(document).ready(function(){

  $('.controlledDocuments textarea').change(function(){
    if($(this).val() == '')
    {
      $('#Field60-0').attr('checked', false); //Yes is unchecked
      $('#Field60-1').attr('checked', true);  //No is checked
    }
    else
    {
      $('#Field60-0').attr('checked', true);  //Yes is checked
      $('#Field60-1').attr('checked', false); //No is unchecked
    }
  });

});  //close document.ready

I'm defaulting the radio button to "No".

When I type something in the field and press TAB, it does change to "Yes" (yay, one win!).

But when I go back up and delete the text, press TAB, it removes the "Yes" but doesn't set the radio button to "No".

Whatever could I be doing wrong????

0 0

Answer

SELECTED ANSWER
replied on October 17, 2018 Show version history

Bert's solution didn't work initially. As Alexander recommended in his alternate solution (which didn't work for me), I changed attr to prop in Bert's initial solution and IT WORKED!!! Great teamwork!

$(document).ready(function(){

  $('.controlledDocuments textarea').change(function(){
    if($(this).val() == '')
    {
      $('#Field60-1').prop('checked', true);  //No is checked
    }
    else
    {
      $('#Field60-0').prop('checked', true);  //Yes is checked
    }
  });

});  //close document.ready

Thank you so much. It's so darn confusing sometimes.

0 0

Replies

replied on October 17, 2018 Show version history

You do not want to set both radiobuttons.  When you set one to checked, it should automatically unchecks the other.

Try this instead

$(document).ready(function(){

  $('.controlledDocuments textarea').change(function(){
    if($(this).val() == '')
    {
      $('#Field60-1').attr('checked', true);  //No is checked
    }
    else
    {
      $('#Field60-0').attr('checked', true);  //Yes is checked
    }
  });

});  //close document.ready

 

0 0
SELECTED ANSWER
replied on October 17, 2018 Show version history

Bert's solution didn't work initially. As Alexander recommended in his alternate solution (which didn't work for me), I changed attr to prop in Bert's initial solution and IT WORKED!!! Great teamwork!

$(document).ready(function(){

  $('.controlledDocuments textarea').change(function(){
    if($(this).val() == '')
    {
      $('#Field60-1').prop('checked', true);  //No is checked
    }
    else
    {
      $('#Field60-0').prop('checked', true);  //Yes is checked
    }
  });

});  //close document.ready

Thank you so much. It's so darn confusing sometimes.

0 0
replied on October 17, 2018

Alternatively, your original JS will work if you change attr to prop

$(document).ready(function(){

  $('.controlledDocuments textarea').change(function(){
    if($(this).val() == '')
    {
      $('#Field2-0').prop('checked', false); //Yes is unchecked
      $('#Field2-1').prop('checked', true);  //No is checked
    }
    else
    {
      $('#Field2-0').prop('checked', true);  //Yes is checked
      $('#Field2-1').prop('checked', false); //No is unchecked
    }
  });

});  //close document.ready
0 0
You are not allowed to follow up in this post.

Sign in to reply to this post.