Skip to content

Commit

Permalink
Adding first files
Browse files Browse the repository at this point in the history
It replaces the select with a text box and gathers the options into spans, but still doesn't display the options
  • Loading branch information
tkellogg committed Sep 1, 2011
0 parents commit 95df9c2
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -0,0 +1 @@
*.swp
35 changes: 35 additions & 0 deletions comboEditable.js
@@ -0,0 +1,35 @@
(function($) {

$.fn.comboEditable = function(opts) {
var name = this.attr('name');
var selectedValue = undefined, selectedText = undefined;
var data = $.map(this.find('option'), function(x) {
if (this.selected) {
selectedValue = this.value;
selectedText = this.innerHTML;
}

return '<span data-value="' + this.value + '" ' +
+ (this.selected ? 'selected' : '') + '>'
+ this.innerHTML + '</span>';
});

if (!selectedText) {
selectedValue = this.find('option').first().val();
selectedText = this.find('option').first().text();
}

var elements = '';
$.each(data, function(x) { elements += x; });

var $ret = $('<div/>');
$ret.append('<input type="text" value="' + selectedText + '" />');
var $text = $ret.find(':text');
$ret.append('<input type="hidden" value="' + selectedValue + '" name="'+name+'" />');
var $value = $ret.find(':hidden');

this.replaceWith($ret);
return $ret;
}

})(jQuery);
19 changes: 19 additions & 0 deletions index.html
@@ -0,0 +1,19 @@
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script type="text/javascript" src="comboEditable.js"></script>
<script type="text/javascript">
$(function() {
$('#demo1').comboEditable();
});
</script>
</head>
<body>
<div id="main">
<select id="demo1">
<option>apples</option>
<option>oranges</option>
</select>
</div>
</body>
</html>

0 comments on commit 95df9c2

Please sign in to comment.