SitePoint Sponsor

User Tag List

Results 1 to 25 of 25

Hybrid View

  1. #1
    SitePoint Guru etidd's Avatar
    Join Date
    May 2011
    Location
    atlanta, ga
    Posts
    623
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    Question Removing Text from Field when It's Activated

    Hello all,

    I am looking to collect email addresses with my site. On my input elements, when the user clicks on it, I want to change the value attribute to blank (make the "your e-mail" disappear when the user clicks on the input element). Is JavaScript used for this?

  2. #2
    Non-Member
    Join Date
    Apr 2011
    Location
    no fixed address
    Posts
    851
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can put an onclick event handler on the element which is called when the user clicks on the element. The event handler then simply changes the value attribute of the element to null or an empty string. This can all be done using javascript.

  3. #3
    SitePoint Guru etidd's Avatar
    Join Date
    May 2011
    Location
    atlanta, ga
    Posts
    623
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Hey, thanks for the post! One more question. Would you recommend putting the script in a separate file, or should I just put it at the bottom of my HTML file?

  4. #4
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,526
    Mentioned
    83 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by etidd View Post
    Hey, thanks for the post! One more question. Would you recommend putting the script in a separate file, or should I just put it at the bottom of my HTML file?
    You should do both.

    Put the script in a separate file so that it can be easily edited by you, as well as being easily cached by the browser.
    Refer to that script file from the end of your body, just before the </body> tag, so that your script can easily work with content on your page before it has finished loading.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  5. #5
    SitePoint Guru etidd's Avatar
    Join Date
    May 2011
    Location
    atlanta, ga
    Posts
    623
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Good suggestion, Paul. I know that I'm gonna be using JS for a little bit more than just this input form function, so I need to start a separate file.

    I've been searching the internet for a while now for help on making the correct statement that will change the value of the value attribute. This is the closest guess I have, and it doesn't work.

    Code:
    name = oForm.elements["name"].value;
    where
    Code:
    oForm = document.forms[index];
    I don't even understand what the 2nd statement means, mainly because of that parameter, [index].
    What JS method should I use?
    & Where's the JS api library?

  6. #6
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,526
    Mentioned
    83 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by etidd View Post
    This is the closest guess I have, and it doesn't work.

    Code:
    name = oForm.elements["name"].value;
    where
    Code:
    oForm = document.forms[index];
    I don't even understand what the 2nd statement means, mainly because of that parameter, [index].
    What JS method should I use?
    The way that I might do that is with this:

    Code html4strict:
    <form id="userDetails">
        <p><label>Name: <input name="name"></label></p>
        ...
    </form>

    Code javascript:
    var form = document.getElementById['userDetails'];
    var name = form.elements['name'].value;

    Quote Originally Posted by etidd View Post
    & Where's the JS api library?
    The one that people seem to prefer starting off with is jQuery, but it's not required. If you are learning JavaScript, it's better if you learn what JavaScript can do first, before doing anything with other API's.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  7. #7
    SitePoint Guru etidd's Avatar
    Join Date
    May 2011
    Location
    atlanta, ga
    Posts
    623
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    I think I have a pretty good image of what JavaScript can do now. It's THE tool for actively updating HTML/CSS code in real-time for a web visitor amongst other things (animation, website layout, etc.).

    But I put this into my code:
    HTML:
    Code:
    <form id="emailbox" name="form1" method="post" action="">
        	<div>
                <input type="text" name="go" id="go" value="your e-mail" onclick="removeText()"/>
                <input type="submit" value="Join" />
            </div>
        </form>
    JS:
    Code:
    function removeText(){
    	var form = document.getElementById['emailbox'];
    	var name = form.elements['go'].value;	
    }
    Now that I have a variable, name, for the value of the value attribute, what function can I use to change the value attribute to blank? I'm still having trouble here.

  8. #8
    Non-Member
    Join Date
    Apr 2011
    Location
    no fixed address
    Posts
    851
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    it depends on how much other javascript you have for the page.

  9. #9
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,597
    Mentioned
    24 Post(s)
    Tagged
    1 Thread(s)
    Give the field an id and reference it using document.getElementById - you need an id to attach the label anyway. The name is for use on the server after the form is submitted.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  10. #10
    SitePoint Guru etidd's Avatar
    Join Date
    May 2011
    Location
    atlanta, ga
    Posts
    623
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Well, I got it figured out.

    Thanks for all your help, guys.
    This is the code I've utilized:
    Code:
    function input_focus(obj){
    	if (obj.value=="your e-mail"){
    		obj.value=""
    	}
    }
    
    function input_reset(obj){
    	obj.value="your e-mail"}

  11. #11
    SitePoint Guru
    Join Date
    Sep 2006
    Posts
    731
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by etidd View Post
    Well, I got it figured out.

    This is the code I've utilized:
    Code:
    function input_focus(obj){
    	if (obj.value=="your e-mail"){
    		obj.value=""
    	}
    }
    
    function input_reset(obj){
    	obj.value="your e-mail"}
    That's what's known as a maintenance nightmare.
    Code:
    function input_focus(obj){
    	if ( obj.value == obj.defaultValue ){
    		obj.value = ""
    	}
    }
    
    function input_reset(obj){
    	obj.value = obj.defaultValue;
    }
    Tab-indentation is a crime against humanity.

  12. #12
    om nom nom nom Stomme poes's Avatar
    Join Date
    Aug 2007
    Location
    Netherlands
    Posts
    10,233
    Mentioned
    47 Post(s)
    Tagged
    1 Thread(s)
    Ali is correct if you have more than one or two inputs with these placeholders. You don't want to have to run a function for every separate input with its own default text.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •