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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T21:48:18+00:00 2026-05-18T21:48:18+00:00

I am having issue in checking which input has been clicked using unobstructive javascript.

  • 0

I am having issue in checking which input has been clicked using unobstructive javascript. I know the method to pass value from function by directly writing it in the input tag but I would like to know how can we pass value from javascript file. As of now adding event to the object is only calling function but not able to send any value

Below is html

    <input id="searchfield" name="searchfield" type="text" value="Your e-mail address" />
<br />
<input id="searchfield2" name="searchfield2" type="text" value="Your password" />

and js code for same I hope you will help me out.

// JavaScript Document
    function removeTextWhenClicked(chk)
    {
        if(chk == 1)
        {
            document.getElementById("searchfield").value = "";
        }
        else if(chk == 2)
        {
            document.getElementById("searchfield2").value = "";
        }
    }
    function removeTextWhenBlur(chk)
    {

        if(chk == 1)
        {
            if(document.getElementById("searchfield").value == "")
            {
                document.getElementById("searchfield").value = "please enter username";
            }
            else
            {
                document.getElementById("searchfield").style.color = "#333333";
            }
        }
        else if(chk == 2)
        {
            if(document.getElementById("searchfield2").value == "")
            {
                document.getElementById("searchfield2").value = "please enter password";
            }
            else
            {
                document.getElementById("searchfield2").style.color = "#333333";
            }
        }
    }
    window.onload = function(){

    document.getElementById("searchfield").onclick = removeTextWhenClicked;
    document.getElementById("searchfield").onblur = removeTextWhenBlur;
    document.getElementById("searchfield2").onclick = removeTextWhenClicked;
    document.getElementById("searchfield2").onblur = removeTextWhenBlur;
    }

Thanks and Regards,
Sagar

  • 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-05-18T21:48:19+00:00Added an answer on May 18, 2026 at 9:48 pm

    The answer to the question you actually asked is that you can do it by building the data into a closure, and then use the closure as your handler:

    document.getElementById("searchfield").onclick = buildHandler(removeTextWhenClicked, 1);
    document.getElementById("searchfield2").onclick = buildHandler(removeTextWhenClicked, 2);
    

    …where buildHandler looks like this:

    function buildHandler(callback, arg) {
        return function() {
            return callback(arg);
        };
    }
    

    More about closures here, they’re a very powerful and useful tool in JavaScript.

    However, in your specific case, you don’t need to do that, because the only information you were passing into the function was a means of determining which element you’re dealing with, and you already have that: this. Within the event handler, this will point to the element on which you set the handler. So for instance, removeTextWhenClicked can be:

    function removeTextWhenClicked()
    {
        this.value = "";
    }
    

    …and since you didn’t need to bind any data to that, you can still use it directly as you were originally.

    Your blur handlers vary more, and so you might need to bind data to them. Alternately, you can make the whole thing markup-driven:

    <input id="searchfield" name="searchfield" type="text" data-msg="please enter username" value="Your e-mail address" />
    <br />
    <input id="searchfield2" name="searchfield2" type="text" data-msg="please enter password" value="Your password" />
    

    Note the new data-img attributes. Now your removeTextWhenBlur becomes:

    function removeTextWhenBlur()
    {
        if (this.value == "")
        {
            this.value = this.getAttribute("data-msg");
        }
        else
        {
            this.style.color = "#333333";
        }
    }
    

    Custom attributes like data-msg are invalid as of HTML4 and earlier (although I’ve never seen a browser that disallowed them) and so that markup won’t validate with validation tools. As of HTML5, custom attributes starting with data- are valid. So you can go ahead and use them now if you don’t use validation as part of your development process [validation is a Good Thing(tm)], or if you use the HTML5 doctype.

    Now, attributes may not be the right thing for you. They can be very useful on large teams where the people doing the markup are different from the people doing the code, and so keeping things fairly decoupled is important. Another approach, though, would map the messages to the elements by their ID. Using your original markup, those functions could work like this:

    var placeholders = {
        "searchfield":  "please enter username",
        "searchfield2": "please enter password"
    };
    
    function removeTextWhenClicked()
    {
        this.value = "";
    }
    function removeTextWhenBlur()
    {
        if (this.value == "")
        {
            this.value = placeholders[this.id];
        }
        else
        {
            this.style.color = "#333333";
        }
    }
    

    Off-topic #1:

    I’d probably change removeTextWhenClicked to:

    function removeTextWhenClicked()
    {
        if (this.value == placeholders[this.id])
        {
            this.value = "";
        }
        this.style.color = ''; // In case we colored it earlier
    }
    

    Off-topic #2:

    It’s well worth using a JavaScript library like jQuery, Prototype, YUI, Closure, or any of several others. They smooth out differences across browser implementations, provide helpful utility functions, and generally save you more time than they consume.

    Off-topic #3:

    The method of handler assignment you’re using, assigning to a property called onclick and such, is called the “DOM0” method. It’s fairly old-fashioned and restrictive. It’s worth looking at the newer “DOM2” way of doing it, which is addEventListener, although sadly it’s attachEvent on IE. (Any JavaScript library will provide a wrapper to deal with that.) One advantage of DOM2 handlers is that more than one handler can be set for the same event on the same element, whereas with the DOM0 mechanism, assigning a new handler bludgeons (removes) any previous one.

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

Sidebar

Related Questions

I'm having an issue with a standard ASP.NET page that has a TextBox and
I'm having an issue with a query that currently uses LEFT JOIN weblog_data AS
I'm having an issue with my regex. I want to capture <% some stuff
I'm having an issue with a Flash/Flex erroring in Firefox but not IE. I
I’m having an issue where a drop down list in IE 6/7 is behaving
I am having an issue where Tomcat is treating extra path information as part
I'm having an issue with CruiseControl.net where the web dashboard just won't work in
I'm having an issue with expressions within reports. I'm coloring the background of a
I'm having an issue dragging a file from Windows Explorer on to a Windows
I'm having an issue setting up one of my projects in TeamCity (v4.0), specifically

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.