Solved

detect when any text field on my form has changed

Posted on 2012-03-19
4
341 Views
Last Modified: 2012-04-09
I have a form with a whole bunch of text fields on it, I would like to detect when any of those fields have changed so that I can some css properties. Right now I have grey text in the fields explaining what it does, one the user clicks on the field, i want the text to disappear and the color to go back to black. is there any easy way to do this?

TIA :)
0
Comment
Question by:Big Monty
  • 2
4 Comments
 
LVL 11

Expert Comment

by:Amar Bardoliwala
ID: 37740826
Hello The_Big_Daddy,

you will need to use onfocus event of textbox as following

<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<p>
 <input name="text1" type="text" id="text1" value="textbox 1" onFocus="this.style.background ='yellow';this.value=''" onBlur="this.style.background='white'">
</p>
<p>
 <input name="text2" type="text" id="text2" value="textbox 2" onFocus="this.style.background = 'yellow'" onBlur="this.style.background='white'">
</p>
</body>
</html>

Open in new window


Hope this will help you.

Thank You.

Amar Bardoliwala
0
 
LVL 20

Accepted Solution

by:
Proculopsis earned 500 total points
ID: 37740901
<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title> - jsFiddle demo</title>
 
  <script type='text/javascript' src='http://code.jquery.com/jquery-1.7.1.js'></script>
 
<style type='text/css'>
    .hint { color: grey; background-color: beige; }
.normal { color: black }

  </style>
 


<script type='text/javascript'>//<![CDATA[

jQuery(document).ready(function() {

    $(".hint[hint]").each(function() {
        $(this).val($(this).attr("hint"))
            });

    $(".hint[hint]").focus(function() {
        if ($(this).val() == $(this).attr("hint")) {
            $(this).val("").toggleClass("hint").toggleClass("normal");
        }
    });

    $(".hint[hint]").blur(function() {
        if ($(this).val() == "") {
            $(this).val($(this).attr("hint")).toggleClass("hint").toggleClass("normal");
        }
    });




});
//]]>  

</script>


</head>
<body>
  <input class="hint" hint="First Name" />
<input class="hint" hint="Last Name" />

</body>
</html>
0
 
LVL 32

Author Comment

by:Big Monty
ID: 37742558
Proculopsis:

Is there any way to make it so I do not have to add classes to all of my text boxes? there a lot of them and they already have classes assigned to them (not the same name) and would require me to rewrite other jQuery code where I'm selecting certain text boxes by class name
0
 
LVL 32

Author Closing Comment

by:Big Monty
ID: 37824427
not the exact solution I added but pointed me in the right direction
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Today I would like to talk about localizing (Internationalization) JavaScript applications. Introduction When creating an application that is going to be used by many people around the globe, it is important to remember that not everyone speak…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now