Solved

detect when any text field on my form has changed

Posted on 2012-03-19
4
339 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
Comment Utility
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
Comment Utility
<!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
Comment Utility
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
Comment Utility
not the exact solution I added but pointed me in the right direction
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…

771 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

10 Experts available now in Live!

Get 1:1 Help Now