How do I a pre-fill a form field for visitors?

Discussion in 'HTML & Website Design' started by Advice Pro, Apr 20, 2010.

  1. #1
    If I make a website with the form on it, I want one of the form fields to be filled so that I can change it when I what.
     
    Advice Pro, Apr 20, 2010 IP
  2. javier.garcia.esteban

    javier.garcia.esteban Well-Known Member

    Messages:
    66
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    118
    #2
    <input type="text" name="field1" value="Here whatever you want displayed by default" />
    <textarea name="field2">Here whatever you want displayed by default</textarea>
     
    javier.garcia.esteban, Apr 20, 2010 IP
  3. Areol

    Areol Peon

    Messages:
    208
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    .

    EDIT HADNT SEEN THE ABOVE POST BUT HAVE GIVEN JAVASCRIPT SOLUTION AS WELL.

    Are you refering to for example on my website i have prepopulated form fields which is simply done with some javascript as you can see here http://www.areol.co.uk/quote/

    What i did was simply have a value for each input field which you can see on mines would be 'Enter Your Name'

    HTML EXAMPLE --

    <input type="text" class="validate[required,custom[onlyLetter]] focusField" name="name" id="name" value="Enter Your Name">
    Code (markup):
    JAVASCRIPT EXAMPLE --

    I am no javascript expert in actual fact my knowledge is limited but what this is doing is simply adding a class .idleField to each input with an input type of text. On click ( focus ) the class .idleField is removed and replaced with .focusField.

    In other words what its doing is displaying all fields values and when you click on a field it then disappears and is replaced by your text. If no text is entered and you click on another field the original value ie 'Enter Your Name' will reappear

    $(document).ready(function() {
    		$('input[type="text"]').addClass("idleField");
           		$('input[type="text"]').focus(function() {
           			$(this).removeClass("idleField").addClass("focusField");
        		    if (this.value == this.defaultValue){ 
        		    	this.value = '';
    				}
    				if(this.value != this.defaultValue){
    	    			this.select();
    	    		}
        		});
        		$('input[type="text"]').blur(function() {
        		$(this).removeClass("focusField").addClass("idleField");
        		    if ($.trim(this.value) == ''){
    			    	this.value = (this.defaultValue ? this.defaultValue : '');
    				}
        		});
    		});
    Code (markup):
    I hope this helps you and anyone else :D
     
    Last edited: Apr 20, 2010
    Areol, Apr 20, 2010 IP