Link to home
Start Free TrialLog in
Avatar of JaXL
JaXL

asked on

Dynamic label that changes based upon event handler

I have a snippet of HTML that is part of a form where there are two input's. Via CSS I have managed to make the label associated with each input to sit inside their corresponding input to act as a placeholder.

Now, with some standard JS (not JQuery I'm afraid), I wanted to capture the event that the user does (i.e. on a mouse click or keyboard press) on the input and dynamically change the look and feel of the label.

http://jsfiddle.net/8nBAQ/2/

The conditions that I need to meet are:

When a user clicks on the input for the first time. The label changes colour to a light grey. After click, if the user then enters a character, the label disappears and the character you have just pressed is displayed instead.

When a user clicks on the input for the first time. The label changes colour to a light grey. After click, if the user then clicks or tabs away from the input, the label changes colour back to it's original state of black.

After entering a few characters into the input, if the user decides to delete the whole set of characters by either pressing backspace deleting each character until none are left or highlighting the whole set of characters with a mouse and presses the delete key, the label appears but is in a light grey colour.

Any help you can offer with the JS would be great!

Thanks
-JaXL
ASKER CERTIFIED SOLUTION
Avatar of Atique Ansari
Atique Ansari
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You would need to set up three events on each input tag.
1. an "onfocus" event to change the color of the label when the input element receives focus (is clicked or tabbed into)
2. an "onkeypress" event to hide the label if the value of the input is not empty (fires every time a key is pressed)
3. an "onblur" event to check whether the input value is empty and unhide the label when the element no longer has focus (click away from or tab out of the element)

#2 and #3 are somewhat redundant but keypress events can be finicky.
Avatar of JaXL
JaXL

ASKER

@atique_ansari: is there a old school JavaScript way around what you have just done? Appreciate that you have come up with a JQuery which looks great and probably a lot better but my hands are tied not to use JQuery.

@xmediaman: is it possible if you could give me an example of what you mean with the event handlers you mention in relation to my conditions?
Here is a simplified version:
<input type="text" name="text1" id="text1" value="" onfocus="javascript: document.getElementById('label1id').style.color='gray';" onkeypress="javascript: if(document.getElementById('text1').value == '') {document.getElementById('label1id).style.display='inline';} else {document.getElementById('label1id).style.display='none';}" onblur="javascript: if(document.getElementById('text1').value == '') {document.getElementById('label1id).style.display='inline';} else {document.getElementById('label1id).style.display='none';} document.getElementById('label1id').style.color='black';">

Open in new window


If you know how, it would be better to define three javascript functions in a script element to call in your input events.