Jump to content

Change Color of Border/Text of Virtual Input Field if Entered Value > Cascading Value


Recommended Posts

I have a submission form that is acting as a little calculator. It has one real field (Month) and a few virtual fields (Days, Crew Size, Hours). The Days virtual field is getting it's number value from cascading off of the Month dropdown field. I want the user to be able to change the number in the Days field, but if they enter in a number value that was greater than the original Cascading Value, then I want to change the border color and text color of the Days field to red. 

I've tried a few things, including this little bit I found on SO, with no luck. 

document.getElementsByClassName('cbFormTextField')[0].oninput = function () {
        var max = parseInt(this.max);

        if (parseInt(this.value) > max) {
            this.style.border = '2px solid red';
            this.style.color = 'red'; 
        }
    }

Any help is appreciated.

Link to comment
Share on other sites

I was able to get it to work, but it's a little glitchy. 

document.getElementsByClassName('cbFormTextField')[0].addEventListener('keyup', applyColorHandler);
  
  function applyColorHandler() {
    let conditionField = document.getElementsByClassName('cbFormTextField')[1];
  
    let virtualField = document.getElementsByClassName('cbFormTextField')[0];
  
      if(parseInt(virtualField.value) > parseInt(conditionField.value)){
        virtualField.style.border = '2px solid red';
        virtualField.style.color = 'red';
      } else {
        virtualField.style.border = '1px solid white';
        virtualField.style.color = 'white';
      }
     };

It seems like there's a state rule on the input field when in focus. The text turns red on keyup, but the border doesn't turn red until you focus out of the input field. Once you re-focus onto it, the border turns white again. 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
×
  • Create New...