Web Design Forum  
 
Go Back   Web Design Forum > Web Software > Scripts

Reply
 
LinkBack Thread Tools Display Modes
Old 12-16-2011, 07:09 PM   #1
New Member
 
Join Date: Dec 2011
Posts: 3
Default Change input field color based on value

I guess as my first post, I should do a little intro. My name is Ryan. I'm 27 and from Michigan. Been into web design since I was about a freshman in high school. Stopped doing it for a while and am now getting back into it. A huge college football fan (go bucks) and baseball (go tribe!).

Anyway, I think it's an easy script but I can't figure it out. I'm still quite basic when it comes to the more advanced coding.

I have a page of about 40 input fields. What I am looking for is a script that will change the background color of the input field based on the value. Example, if the field is blank, the background remains white. If someone writes the word "OK" in it, I want it to turn green. If they write "NO" in it, I want it to turn red. I figure this is a jQuery thing which I've not studied yet. Any help would be appreciated!

Ryan
rybussell is offline   Reply With Quote


Old 12-28-2011, 03:03 PM   #2
Gold Member
 
conor's Avatar
 
Join Date: Oct 2008
Location: Ireland
Posts: 349
Default

Hi Ryan,

jQuery makes things like this very easy. You could try something like this:

Code:
// this means execute when the page has loaded
$(function(){

  // add an event trigger, which executes this function
  // when someones typing in an input
  $( 'input' ).keyup( function( ){
    var $this = $( this ); 

    // get the value of the input and switch between
    // its possible values, changing the color if it matches
    // ok or no. 
    var val = $this.val( );
    switch( val ){
      case "OK":
        $this.css( { "background-color" : "green" } );
      break;
      case "NO":
        $this.css( { "background-color" : "red" } );
      break;
      default:
        $this.css( { "background-color" : "#fff" } );
     }
  });
});
Hope that helps, let me know if you have any questions.
__________________
Conor
conor is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT. The time now is 10:49 AM.


Camera Forum - Computer Forum - Web Design Forum

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
Content Relevant URLs by vBSEO 3.6.0 ©2011, Crawlability, Inc.