Using jQuery to convert text into form fields (2)

This post is more than 2 years old.

Many months ago I posted a blog entry describing how to use jQuery to turn a text item into a drop down. Read the older entry for more information on what I mean, but the basic gist is - you click an edit link and a piece of text turns into a drop down. This worked fine but a user asked about converting the drop down back into plain text. I worked on it a bit and it turns out it isn't that difficult.

To begin with, I added a new event listener to handle the change event in my drop down. Because the drop downs were created after the document was loaded, I had to use the live() feature. Note - before jQuery 1.4 it was not possible to use live with the change event. If you plan on using my code, be sure you are using the latest jQuery build. Here is the event handler I used:

$(".selector").live("change", function() { //first get the value var val = $(this).val() var name = $(this).attr("name") var s = "<input type=\"hidden\" name=\"" + name + "\" value=\"" + val +"\">" s += labels[val-1] + " - " s += "<a href=\"\" class=\"edit\">edit</a>" $(this).parent().html(s) })

As you can see, I simply grab the value and name from the drop down and recreate the text that was there originally. I had to make one more tweak. Since I was now generating edit links on the fly as well, I changed my original click listener for them to a live as well. So basically, I've got two live events. One to listen for "Edit" clicks, one to listen for "Change" events. You can play with a demo of this here. Here is the complete source.


<head> <script type="text/javascript" src=""></script> <script>

$(document).ready(function() {

//used for our drop downs
var values = [1,2,3,4]
var labels = ["Hated It","Disliked It","Liked It","Loved It"]

$(".changeable a.edit").live("click",function() {
	//find the hidden item
	var hiddenItem = $("input:hidden", $(this).parent())
	//get the current val
	var currentVal = hiddenItem.val()
	//get the name
	var currentName = hiddenItem.attr("name");
	//now we can draw our drop down and select the right val
	var s = "&lt;select class=\"selector\" name=\""+currentName+"\"&gt;";
	//hard coded values for our drop down
	for(var i=0;i&lt;values.length;i++) {
		s+= "&lt;option value=\"" + values[i] + "\""
		if(currentVal == values[i]) s+= " selected"
		s+= "&gt;" + labels[i] + "&lt;/option&gt;"
	s += "&lt;/select&gt;"
	//now replace
	return false	

$(".selector").live("change", function() {
	//first get the value
	var val = $(this).val()
	var name = $(this).attr("name")
	var s = "&lt;input type=\"hidden\" name=\"" + name + "\" value=\"" + val +"\"&gt;"
	s += labels[val-1] + " - "
	s += "&lt;a href=\"\" class=\"edit\"&gt;edit&lt;/a&gt;"


</script> </head>


<form method="post">

star wars: <span class="changeable"><input type="hidden" name="starwars" value="4">Loved It - <a href="" class="edit">edit</a></span><br/> star trek: <span class="changeable"><input type="hidden" name="startrek" value="3">Liked It - <a href="" class="edit">edit</a></span><br/>

<input type="submit" value="click me like you mean it"> </form>

<cfif not structIsEmpty(form)> <cfdump var="#form#" label="form"> </cfif>

</body> </html>

Raymond Camden's Picture

About Raymond Camden

Raymond is a senior developer evangelist for Adobe. He focuses on document services, JavaScript, and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support. You can even buy me a coffee!

Lafayette, LA

Archived Comments

Comment 1 by Dan G. Switzer, II posted on 3/19/2010 at 10:51 PM

While not exactly what you've done, if you're looking for a more unobtusive result, a while back I created a jQuery plug-in which converts a select element into a link w/dropdown menu:

There's a bunch of examples here:

The plug-in may be exactly what some people are requiring and all you need to get it to work is attach it to existing select elements on your page.

Comment 2 by Raymond Camden posted on 3/20/2010 at 12:36 AM

Ok, that's a pretty sexy plugin there.

Comment 3 by Dave posted on 6/22/2013 at 12:19 PM

using jquery 1.10.1, doesnt seem to work?

Comment 4 by Raymond Camden posted on 6/22/2013 at 4:46 PM

The .live API is deprecated, that's why. Change to .on.