Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8559109
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T15:57:53+00:00 2026-06-11T15:57:53+00:00

I am new with html and javascript. I have been trying to modify the

  • 0

I am new with html and javascript. I have been trying to modify the value of an input element with javascript. The purpose is to set the field blank when user clicks it. the code is as below:

<html>
<head>
<script>
function fn(){
if(document.input1.value=="name")
document.input1.value="";
}</script></head>
<body>
<input name="input1" type="text" value="name" onClick="fn()"/>
</body></html>

However, this code is not working. I am using a chrome browser. When I surround the input tag with a form tag and in script I use document.form.input1.value the code works. where is the problem? I am not concerned about how to blank the field on user click, rather how to access the value from a function. Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T15:57:55+00:00Added an answer on June 11, 2026 at 3:57 pm

    You may also want to try the onFocus and onBlur events:

    <html>
      <head>
       <script>
         function fn(){
           if(document.getElementById("input1").value=="name")
             document.getElementById("input1").value="";
         }
         function fn2(){
           if(document.getElementById("input1").value=="")
             document.getElementById("input1").value="name";
         }
        </script>
      </head>
      <body>
       <input id="input1" name="input1" type="text" value="name" onFocus="fn();" onBlur="fn2();"/>
      </body>
    </html>​
    

    EXAMPLE

    EDIT:

    If you prefer to get the element by its name, you can use document.getElementsByName, however, this returns a list of all matching elements and you would then treat it as an array of matched elements. In the case above, you would switch document.getElementById("input1") with document.getElementsByName("input1")[0]

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am new with JavaScript, I have a value in an input field, like
I am very new to javascript and ajax/jquery and have been working on trying
Im quite new at Javascript however I have been trying to create a currency
I am new to Javascript (and programming in general) and have been trying to
I have a page that i have been starting to setup here http://www.brandybrowauto.com/NEW.html that
I've been trying to modify a Google charts example in order to have three
I am new to XML and have been trying some simple examples and they
I'm new here (and to web development in general), and I have been trying
I'm very new to javascript and have been playing around with regular expressions. I'm
I am new to JavaScript and jQuery, but have used HTML and CSS before.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.