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?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
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.
Introduction
Fairly simple, yes. I can think of two basic approaches:
contenteditableattributeinputyou add on-the-flyHandy references for both of the below:
Using the
contenteditableattributeThe
contentEditableattribute (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):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
innerHTMLand Ajax.)If you want to grab the (edited) content, you just grab
innerHTMLfrom the elements you’ve made editable. Here’s an example of some JavaScript that will flag up whencontenteditablespansblur(live copy):Using an
inputyou add on-the-flyYou need to get a reference to the element that you’re using for display (a
span, perhaps) and then hook itsclickevent (or hook theclickevent on a parent of the desired element(s)). In theclickevent, hide thespanand insert ainput[type=text]alongside it.Here’s a very simple example of using an
input:There I’m hooking the
clickon the parentpelement, 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
blurto 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
addEventListenerand so you have to fall back toattachEvent.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.