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 01-26-2010, 02:55 PM   PM User | #1
many_tentacles
Regular Coder

 
Join Date: Dec 2005
Location: UK
Posts: 207
Thanks: 6
Thanked 2 Times in 2 Posts
many_tentacles is an unknown quantity at this point
if statements and jquery not working

I have some code which works fine in traditional javascript (see code below) but I need to work by referencing the class rather than the id so I've tried to create the same functionality using jquery.

The "if" part of the statement isn't really working properly though. not sure why though.

Any help would be much appreciated.


Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.0/jquery.min.js"></script>
<script type="text/javascript" language="javascript">

$().ready(function() {
    $(".myTextBox").focus(function() {  
  		if ($(".myTextBox").attr("Value", "test")) { 
			$(".myTextBox").attr("Value", "");
			$(".myTextBox").css("color", "black");
  		}
   	});
	
	$(".myTextBox").blur(function() {  
  		if ($(".myTextBox").attr("Value", "")) { 
			$(".myTextBox").attr("Value", "test");
			$(".myTextBox").css("color", "green");
  		}
   	});
});





function clearbox() {
	if (document.getElementById('myTextBox2').value == 'test') 
		{ 
		document.getElementById('myTextBox2').value = ''; document.getElementById('myTextBox2').style.color = '#000';
		}	
}

function fillbox() {
	if (document.getElementById('myTextBox2').value == '') 
		{ 
		document.getElementById('myTextBox2').value = 'test'; document.getElementById('myTextBox2').style.color = 'green';
		}	
}

</script>

</head>

<body>
This does not work<br />
<input name="" class="myTextBox" type="text" value="test" style="color:green;" />
<br /><br /><br />
This works<br />
<input name="" id="myTextBox2" type="text" value="test" onfocus="clearbox()" onblur="fillbox()" style="color:green;" />

</body>
</html>
many_tentacles is offline   Reply With Quote
Old 01-26-2010, 04:33 PM   PM User | #2
Fumigator
UE Antagonizer


 
Fumigator's Avatar
 
Join Date: Dec 2005
Location: Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
Posts: 7,686
Thanks: 42
Thanked 637 Times in 625 Posts
Fumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of light
the attr() function is case-sensitive, so you must use .attr("value"). But, you can make it even easier and use jQuery's built-in .val().

http://api.jquery.com/val/
__________________
Fumigator 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:36 PM.


Advertisement
Log in to turn off these ads.