0

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.

3
  • You are not linking to a web page. Commented Sep 1, 2010 at 8:34
  • @Pekka — stackoverflow.com/questions/3616227/javascript-help :) Commented Sep 1, 2010 at 8:36
  • @David aaaah, literally this webpage! I see. Commented Sep 1, 2010 at 8:51

4 Answers 4

2

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!

Sign up to request clarification or add additional context in comments.

Comments

0

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

http://wmd-editor.com/

This does exactly what you're looking for.

Comments

0

Look at jquery:

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

Thats a start. :)

2 Comments

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.
edited my original answer... had a semi colon in the wrong place.
0

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

4 Comments

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: 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.
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: password fields do not display their text value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.