From 988323b1d07bc0b87e7749f81af26f6bba60ec62 Mon Sep 17 00:00:00 2001 From: Alex Sanford Date: Sun, 28 Aug 2011 22:37:16 -0300 Subject: [PATCH] Extracted DOM option creation into function to apply IE workaround --- public/javascripts/admin/custom_fields/has_many.js | 9 ++++----- public/javascripts/admin/utils.js | 9 ++++++++- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/public/javascripts/admin/custom_fields/has_many.js b/public/javascripts/admin/custom_fields/has_many.js index f4ee017e..3e5dc5eb 100644 --- a/public/javascripts/admin/custom_fields/has_many.js +++ b/public/javascripts/admin/custom_fields/has_many.js @@ -11,10 +11,10 @@ $(document).ready(function() { if (context.data.new_item) { var newItemInfo = context.data.new_item; - var option = new Option(newItemInfo.label, newItemInfo.url, true, true); + var option = makeOption(newItemInfo.label, newItemInfo.url, true, true); context.select.append(option); - context.select.append(new Option('-'.repeat(newItemInfo.label.length), '', false, false)); + context.select.append(makeOption('-'.repeat(newItemInfo.label.length), '', false, false)); } for (var i = 0; i < context.data.collection.length; i++) { @@ -27,7 +27,7 @@ $(document).ready(function() { for (var j = 0; j < obj.items.length; j++) { var innerObj = obj.items[j]; if ($.inArray(innerObj[1], context.data.taken_ids) == -1) { - optgroup.append(new Option(innerObj[0], innerObj[1], false, false)); + optgroup.append(makeOption(innerObj[0], innerObj[1], false, false)); size++; } } @@ -36,8 +36,7 @@ $(document).ready(function() { } else { if ($.inArray(obj[1], context.data.taken_ids) == -1) { - var option = new Option("", obj[1], false, false); - $(option).text(obj[0]); + var option = makeOption(obj[0], obj[1], false, false); context.select.append(option); } } diff --git a/public/javascripts/admin/utils.js b/public/javascripts/admin/utils.js index ed98db00..7c11e7fb 100644 --- a/public/javascripts/admin/utils.js +++ b/public/javascripts/admin/utils.js @@ -30,4 +30,11 @@ Object.size = function(obj) { if (obj.hasOwnProperty(key)) size++; } return size; -}; \ No newline at end of file +}; + +// Make a DOM option for a select box. This code works around a bug in IE +function makeOption(text, value, defaultSelected, selected) { + var option = new Option('', value, defaultSelected, selected); + $(option).text(text); + return option; +}