Go Back   CodingForums.com > :: Client side development > JavaScript programming > JavaScript frameworks

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-14-2009, 03:59 AM   PM User | #1
Jon W
Regular Coder

 
Join Date: Jan 2008
Posts: 334
Thanks: 9
Thanked 0 Times in 0 Posts
Jon W is an unknown quantity at this point
Onfocus and onblur

The title pretty much says it all. I'm trying to develop a script when its focused and when it goes then onblur if the value is empty it will change the text color of the title of the input.

So normally this was a pretty simple and easy question to answer. Though I guess my biggest problem that I'm facing right now is that I'm trying to do all this for all of my inputs on this form into one function. I would perfer not have to put it in any function but I don't yet have enough knowledge to know how to do that yet... So here is my code.


Code:
	<script type="text/javascript">
		
		function chckFields(){
			
		var addMMO = document.addMMO;
		var empty = '';
			
			if (addMMO.gametitle.value == empty) {
				document.getElementById('gametitle').style.color = 'red';
			}
			else
			 {
			 	document.getElementById('gametitle').style.color = 'green';
			 }
			 
			 if(focus.addMMO.os.value == 0) {
			 	document.getElementById('os').style.color='red';
			 }
			 else
				 {
				 document.getElementById('os').style.color='green';	
				 }
		}
	</script>
And here is my html

Code:
			<label for="gameTitle" id="gametitle">Game Title: </label>
			<input name="gametitle" onblur="chckFields();" type="text" title="Provide a game title that you are reviewing" />
			
			<label for="os" id="os">Operating Systems: </label>
			<select name="os" onblur="chckFields();">
				<option value="0">Choose</option>
				<optgroup label="------------------------------------">
				<option value="1">Windows</option>
				<option value="2">Mac</option>
				<option value="3">Linux</option>
				<option value="4">Windows . Mac</option>
				<option value="5">Windows . Mac . Linux</option>
				<option value="6">Linux . Mac</option>
				<option value="7">Windows . Linux</option>
				</optgroup>
			</select>
So the problem is that since all of these are in one function as soon as the first one acts it goes and checks all of them before the user has even got the chance to click on them. So the way that it's sets now if you were to click on the first input and then click away off of the input the game title and the OS title would turn red.

My goal was to do it for each and every input. So if you clicked on the first one and then left it without filling anything in it would turn the text red and stay red until you filled something in, and if you went down to the other one and just click the select box and left without selecting anything then it would turn red. I do not when you just click on one they all turn red.

Do you follow me?

So first thought was well I could put then inside of separate functions. But then I thought that would be a little crazy making a new function for each and every one of my inputs. There has to be a better way.... What is the cleanest and more effective way to do this?
Jon W is offline   Reply With Quote
Old 08-17-2009, 05:53 PM   PM User | #2
Spudhead
Senior Coder

 
Spudhead's Avatar
 
Join Date: Jun 2002
Location: London, UK
Posts: 1,856
Thanks: 8
Thanked 110 Times in 109 Posts
Spudhead is on a distinguished road
Well, seeing as you've posted this in the "Javascript Frameworks" section, then I'll start by saying that the cleanest and most effective way do to it is using a Javascript framework of some kind. My personal suggestion would be jQuery. More specifically, the fantastically useful jQuery validation plugin.

As a quick and dirty example of how it could work in jQuery:

Code:
$(document).ready(function() {

	$('input').blur(function(){
		var color = ($(this).val() == "") ? "red" : "green";
		$(this).prev("label").css("color", color);
	});

});
Spudhead is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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

Forum Jump


All times are GMT +1. The time now is 11:55 AM.


Advertisement
Log in to turn off these ads.