Solved

detect when any text field on my form has changed

Posted on 2012-03-19
4
345 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 33

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 33

Author Closing Comment

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

Featured Post

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

Suggested Solutions

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…

752 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