views:

54

answers:

4

Hi, I am very new to javascript. I want to add the same effect as that given in the text fields of this webpage in my own page... Can any one help me with how to do it or where to learn it from.

I want my text fields to show what have to be written there but it will change as soon as I write something in it..

Another thing is that a small popup block will appear when I click on a textbox which describes what and how to write in it... like password should be alpha numeric... or more than 6 character long ect..

Thanks...

A: 

Look at jquery:

$('#textarea').onKeyup(
    function(){
        $('div#mydiv').innerHTML( $(this).val() );
    }
);

Thats a start. :)

Thomas Clayson
whats the job of this innerHTML can u please explain me...
Alivia
innerHTML lets you get or change the inner HTML of an element. So, if you have a div with "Hello World" in it then doing `alert($('div#mydiv').innerHTML());` will alert "Hello World". Similarly if you give a string you can change the inner HTML of the element. so doing `$('div#mydiv').innerHTML('Goodbye Universe');` will change the "hello world" to "Goodbye Universe". Putting $(this).val() instead of text will return the current value of the textarea with id "#textarea", and will get the "as you type" functionality you're looking for.
Thomas Clayson
edited my original answer... had a semi colon in the wrong place.
Thomas Clayson
+2  A: 

If you want to do this in an accessible way (and you should):

If JS is available, position a transparent input over a label inside a container and toggle the transparency of the input based on its value at document load time and whenever the focus enters of leaves the element.

I've produced a minimal example.

As for the second part of the question. That's very similar. Just have another label beside the input, and toggle its visibility when the focus enters or leaves, ignoring the value of the input. I wouldn't bother with this though, advance notice of requirements is nice!

David Dorward
A: 

You might want to take a look at the WMD markdown editor:

http://wmd-editor.com/

This does exactly what you're looking for.

Paddy
A: 

HTML Input field

<input type="text" name="name" id="name" value="" />

Javascript

function hint () {
  var elem = document.getElementById('name'); // get element

  elem.value = "Enter Name"; // fill element with value

  elem.onfocus = function () {   // if the user focuses 
    if (elem.value === "Enter Name") {  // if there is default text 
        elem.value = ""; // clear it
    }
  }

  elem.onblur = function () { // if user removes focus on field
    if (elem.value === "") {   // if the field is empty 
        elem.value= "Enter Name"; // fill in a default value
    }  
  } 
}

window.onload = hint;  // call the function when the page has loaded
Q_the_dreadlocked_ninja
hii,this is really helpful....
Alivia
@Alivia, please tick the answer if it worked for you.
Q_the_dreadlocked_ninja
This approach has some accessibility problems. If JS isn't available, the user doesn't get any information about what they are supposed to put in it. If the focus arrives on the element, then that information vanishes … and screen readers don't start to read elements until they have the focus. Best to leave values for values and labels for labels.
David Dorward
@David: any JavaScript developer should take that fact into account before writing any piece of code - i am just providing a JavaScript solution to what he is asking for. Anywhere you are right, you cannot create input fields without labels, JS should only be an enhancement.
Q_the_dreadlocked_ninja
hi,actually i am dealing with a textbox which is performing a password change..i want to white "password must me 6 character long there" but when some one types on it the textmode should change to password any idea how can i do this........
Alivia
@Alivia: password fields do not display their text value.
Q_the_dreadlocked_ninja