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 8854027
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T13:47:21+00:00 2026-06-14T13:47:21+00:00

I’ve found many examples online on how to clear the same text field you

  • 0

I’ve found many examples online on how to clear the same text field you click on, but I’d like the user to click on text field 1 and it clear text field 2, and vice versa.

Been struggling on this for hours. The problem I’m having is I’m having no errors, no output, and it doesn’t work. I have no idea what the problem is.

My code:

<jsp:useBean id="speed" class="java.lang.String" scope="request" />
<jsp:useBean id="force" class="java.lang.String" scope="request" />

<script>
    function clearSpeed() {
        var x = document.getElementByID(speed);
        x.value = "";
    }

    function clearForce() {
        var x = document.getElementByID(force);
        x.value = "";
    }
</script>

<input type="text" id="speed" onfocus='clearForce()' value="<%=speed%>">

<input type="text" id="force" onfocus='clearSpeed()' value="<%=force%>">
  • 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-14T13:47:22+00:00Added an answer on June 14, 2026 at 1:47 pm

    You need to quote your strings, and JavaScript is case-sensitive – the function is getElementById() :

    function clearSpeed() {
        var x = document.getElementById('speed'); // added quotes around the id
        x.value = "";
    }
    
    function clearForce() {
        var x = document.getElementById('force'); // added quotes around the id
        x.value = "";
    }
    

    Better would be this :

    <input type="text" id="speed" onfocus="clearValue('force')" value="<%=speed%>">
    <input type="text" id="force" onfocus="clearValue('speed')" value="<%=force%>">
    

    Calls a single function (I also changed the quotes round for better consistency)

    Updated JavaScript:

    function clearValue(id) {
        var x = document.getElementById(id); // no need for quotes and value passed in as variable
        x.value = "";
    }
    

    This promotes re-usable code and less duplication

    var x = document.getElementById(id);
    x.value = "";
    

    could become (if you really wanted to make it shorter !)

    document.getElementById(id).value = "";
    

    There is no need to use a variable, unless of course you want to do other things with the DOM element

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

Sidebar

Related Questions

I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I need to clean up various Word 'smart' characters in user input, including but
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I've got a string that has curly quotes in it. I'd like to replace
I would like to run a str_replace or preg_replace which looks for certain words
I have a French site that I want to parse, but am running into
I am trying to render a haml file in a javascript response like so:

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.