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

Question

Question

JavaScript help for field - drop down field, if Yes, then red

asked on August 23, 2019

Could someone help me with JavaScript coding?  I have an Urgent? field with two drop down choices, Yes or No, and I would like the field input to be bold and red if the choice is switched to Yes.

I'm thinking JavaScript because trying it with CSS is messing up my spacing on my form (and I wasn't successful anyways).  I had tried this from another Answers post that I found:  

 

0 0

Replies

replied on August 25, 2019

Hi Connie,

I'm not a JavaScript expert myself but I used this to do something similar to what you describe.

//Tuesday Time Check
$(document).ready(function(){
  
    $(".T2 select").on('change', function() {
      
      var val =$('.T2 select').val();
      alert(val);
      if (val == ''){
        $('.TStartTime2 input').val('');
        $('.TEndTime2 input').val('');
      }
                                  
    });
  
  $(document).on('change', function(){
    var TEndTime2 = $(".TEndTime2 input").val();
    var TStartTime1 = $(".TStartTime1 input").val();
    var TEndTime1 = $(".TEndTime1 input").val();
    var TStartTime2 = $(".TStartTime2 input").val();
    var failed = false;
    var Activty2 = $(".Activity2 select").val();
    if (TStartTime2 >= TStartTime1 && TStartTime2 < TEndTime1){
      $('.TEndTime2 input').css('background-color', 'red');
      $('.Submit').attr('disabled', 'disabled');
      failed = true;
    }
    else if (TStartTime1 >= TStartTime2 && TStartTime1 < TEndTime2){
      $('.TEndTime1 input').css('background-color', 'red');
      $('.Submit').attr('disabled', 'disabled');
      failed = true;
    }

    else {
      $('.Submit').removeAttr('disabled');
      $('.TEndTime1 input').css('background-color', 'white');
      $('.TEndTime2 input').css('background-color', 'white');
    }     
  });
  

  
});

 

0 0
replied on September 5, 2019

Thanks, Johnathan.  Back from holidays now and looking at this again.  Your JavaScript:  That's a little bit scary to me!  :-)  I would need to change it far beyond what I'm familiar with (no JavaScript training - I wish I had time to take some JS training!).

I managed to make this work somewhat (see below - stolen from another post).  It changes the background to yellow upon ANY change to the field (the red was too dark for a background).  The default is No, so any change to Yes makes the background yellow, which makes the field more noticeable.  So, the goal of drawing attention to it is achieved.

$(document).ready(function(){
  $('.dropdown').on('change', 'select', changeColor); 
  function changeColor () {
    if ($(this).val() != '') {
      $(this).css('background','#ffffd0');
    }
    else {
      $(this).css('background','none');
    }     
  }
});

Gets me this:  

0 0
replied on September 5, 2019 Show version history

And a slight change in this code allowed me to have the background turn yellow only if the drop down choice was Yes:

$(document).ready(function(){
  $('.dropdown').on('change', 'select', changeColor); 
  function changeColor () {
    if ($(this).val() != 'No') {
      $(this).css('background','#ffffd0');
    }
    else {
      $(this).css('background','none');
    }     
  }
});

1 0
You are not allowed to follow up in this post.

Sign in to reply to this post.