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

  • Home
  • SEARCH
  • 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 6193247
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T03:05:10+00:00 2026-05-24T03:05:10+00:00

I’m trying to make a page that has some editabable fields, but I only

  • 0

I’m trying to make a page that has some editabable fields, but I only want them to display as input boxes once the user clicks on them (the rest of the time showing as plain text). Is there a simple way to do this in Javascript?

  • 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-24T03:05:10+00:00Added an answer on May 24, 2026 at 3:05 am

    Introduction

    Fairly simple, yes. I can think of two basic approaches:

    • Using the contenteditable attribute
    • Using an input you add on-the-fly

    Handy references for both of the below:

    • DOM2 Core
    • DOM2 HTML
    • DOM3 Core
    • HTML5 spec – “user interaction” section

    Using the contenteditable attribute

    The contentEditable attribute (W3C, MDC, MSDN) can be “true” indicating that the element can be edited directly. This has the advantage of not requiring any JavaScript at all (live example):

    <p id="container">The <span contenteditable="true">colored items</span> in this paragraph
    are <span contenteditable="true">editable</span>.</p>
    

    Lest you think this is some l33t new thing, IE has supported it since IE 5.5 and other major browsers for very nearly that long. (In fact, this was one of many Microsoft innovations from the IE5.5 / IE6 timeframe; they also gave us innerHTML and Ajax.)

    If you want to grab the (edited) content, you just grab innerHTML from the elements you’ve made editable. Here’s an example of some JavaScript that will flag up when contenteditable spans blur (live copy):

    var spans = document.getElementsByTagName("span"),
        index,
        span;
    
    for (index = 0; index < spans.length; ++index) {
        span = spans[index];
        if (span.contentEditable) {
            span.onblur = function() {
                var text = this.innerHTML;
                text = text.replace(/&/g, "&amp").replace(/</g, "&lt;");
                console.log("Content committed, span " +
                        (this.id || "anonymous") +
                        ": '" +
                        text + "'");
            };
        }
    }
    #container span {
        background-color: #ff6;
    }
    <p id="container">The <span id="span1" contenteditable="true">colored items</span> in this paragraph 
        are <span contenteditable="true">editable</span>.</p>

    Using an input you add on-the-fly

    You need to get a reference to the element that you’re using for display (a span, perhaps) and then hook its click event (or hook the click event on a parent of the desired element(s)). In the click event, hide the span and insert a input[type=text] alongside it.

    Here’s a very simple example of using an input:

    window.onload = function() {
        document.getElementById('container').onclick = function(event) {
            var span, input, text;
    
            // Get the event (handle MS difference)
            event = event || window.event;
    
            // Get the root element of the event (handle MS difference)
            span = event.target || event.srcElement;
    
            // If it's a span...
            if (span && span.tagName.toUpperCase() === "SPAN") {
                // Hide it
                span.style.display = "none";
    
                // Get its text
                text = span.innerHTML;
    
                // Create an input
                input = document.createElement("input");
                input.type = "text";
                input.value = text;
                input.size = Math.max(text.length / 4 * 3, 4);
                span.parentNode.insertBefore(input, span);
    
                // Focus it, hook blur to undo
                input.focus();
                input.onblur = function() {
                    // Remove the input
                    span.parentNode.removeChild(input);
    
                    // Update the span
                    span.innerHTML = input.value == "" ? "&nbsp;" : input.value;
    
                    // Show the span again
                    span.style.display = "";
                };
            }
        };
    };
    #container span {
        background-color: #ff6;
    }
    <p id="container">The <span>colored items</span> in this paragraph
        are <span>editable</span>.</p>

    There I’m hooking the click on the parent p element, not the individual spans, because I wanted to have more than one and it’s easier to do that. (It’s called “event delegation.”) You can find the various functions used above in the references I gave at the beginning of the answer.

    In this case I used blur to take the edit down again, but you may wish to have an OK button and/or other triggers (like the Enter key).


    Off-topic: You may have noticed in the JavaScript code above that I had to handle a couple of “MS differences” (e.g., things that IE does differently from other browsers), and I’ve used the old “DOM0” style of event handler where you just assign a function to a property, which isn’t ideal, but it avoids my having to handle yet another difference where some versions of IE don’t have the DOM2 addEventListener and so you have to fall back to attachEvent.

    My point here is: You can smooth over browser differences and get a lot of utility functions as well by using a decent JavaScript library like jQuery, Prototype, YUI, Closure, or any of several others. You didn’t say you were using any libraries, so I didn’t in the above, but there are compelling reasons to use them so you don’t have to worry about all the little browser niggles and can just get on with addressing your actual business need.

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

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am trying to understand how to use SyndicationItem to display feed which is
I need to clean up various Word 'smart' characters in user input, including but
i want to parse a xhtml file and display in UITableView. what is the
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have just tried to save a simple *.rtf file with some websites and
I'm new to using the Perl treebuilder module for HTML parsing and can't figure

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.