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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T07:35:11+00:00 2026-05-14T07:35:11+00:00

When the user sets the focus to an edit control on my HTML page,

  • 0

When the user sets the focus to an edit control on my HTML page, I want some other text to appear “around” it, to give some hints for how to fill out the edit control, and some buttons to fill it with random data. What’s the best way to do this with javascript or jquery or whatever?

Thanks.

  • 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-14T07:35:11+00:00Added an answer on May 14, 2026 at 7:35 am

    You can hook the focus and blur events of the control, and use them to show and hide the elements you want to show and hide.

    Example 1 – Unobtrusive

    You can do this without putting Javascript in your markup by using IDs and DOM methods. For instance, given this markup:

    <input type='text' id='userNameField' name='userNameField'>
    <span style='display: none' id='userNameFieldHelp'>
    User names may contain the characters A-Z, 0-9, and _.
    </span>
    

    You can hook the events using your favorite Javascript library (jQuery, Prototype, Closure, etc., etc.). Or here’s a lame example not using any of them:

    window.onload = pageInit;
    function pageInit() {
        var field = document.getElementById('userNameField');
        field.onfocus = fieldFocus;
        field.onblur = fieldBlur;
    }
    
    function fieldFocus() {
    
        handleHelp(this.id, true);
    }
    
    function fieldBlur() {
    
        handleHelp(this.id, false);
    }
    
    function handleHelp(fieldId, show) {
        var span;
    
        if (fieldId) {
            span = document.getElementById(fieldId + "Help");
            span.style.display = show ? 'inline' : 'none';
        }
    }
    

    I wouldn’t literally do it that way (I’d generalize things, use a library to make it easy to look things up by attribute, use an attribute to relate things rather than IDs and a naming convention, etc., etc.), but you get the idea.

    Example 2 – Unobtrusive and general, using Prototype

    Just by way of example, here’s an approach using Prototype which is much more generic. A field and its help are related by the field name and the help element’s data-help-for attribute.

    HTML:

    <input type='text' name='userNameField' class='helpable'>
    <span style='display: none' data-help-for='userNameField'>
    User names may contain the characters A-Z, 0-9, and _.
    </span>
    

    Javascript:

    document.observe('dom:loaded', pageInit);
    function pageInit() {
        var list;
    
        // Get a list of all "helpable" fields
        list = $$('.helpable');
    
        // Watch for focus and blur
        list.observe('focus', handleHelp.curry(true));
        list.observe('focus', handleHelp.curry(false));
    }
    function handleHelp(show) {
        var help;
    
        // Get all of the elements that are help for this field
        help = $$('*[data-help-for=' + this.name + ']');
    
        // Show/hide all of them
        help.invoke(show ? 'show' : 'hide');
    }
    

    Example 3 – Intermixed and fragile

    Just to show a range of options, you can also intermix your Javascript and HTML, and use relative methods (which are more fragile — if you change your display slightly, you can break things pretty easily). But:

    HTML:

    <input type='text' id='userNameField' name='userNameField'
        onfocus='handleHelp(this, true)'
        onblur='handleHelp(this, false)'
    >
    <span style='display: none' id='userNameFieldHelp>
    User names may contain the characters A-Z, 0-9, and _.
    </span>
    

    Script:

    function handleHelp(element, show) {
        var span;
    
        // Find the next span
        for (span = element; span; spen = span.nextSibling) {
            if (span.nodeType == 1 && span.tagName == 'SPAN') {
                break;
            }
        }
    
        // Show or hide it
        if (span) {
            span.style.display = show ? 'inline' : 'none';
        }
    }
    

    But again, the above is very fragile to the structure — put in another span for whatever reason between the field and its help, and the wrong one gets shown/hidden.

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

Sidebar

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.