Brandon MUS
11-07-2006, 12:18 AM
I'm working on a script, and JavaScript isn't my forte by any means. The hopes of the script is to create a list of <input type="radio" />'s, but I'm struggling in IE. I was using innerHTML, but after reading about it, I think I should switch over to DOM (which is why I'm here). My script is pretty straight forward:
//add_color will append a color option to the list
function add_color(colorIndex) {
var colorsContainer = document.getElementById('product_colorsContainer');
var HTML = colorsContainer.innerHTML;
HTML += " <tr>";
HTML += " <td>";
HTML += " <label for=\"product_color["+colorIndex+"]\">";
HTML += " <input type=\"radio\" value=\""+colorsArray[colorIndex][0]+"\" name=\"product_colors\" id=\"product_color["+colorIndex+"]\" onclick=\"javascript:setTimeout('update_sizes("+colorIndex+")', 50);\" /> "+colorsArray[colorIndex][1];
HTML += " </label>";
HTML += " </td>";
HTML += " </tr>";
colorsContainer.innerHTML = HTML;
}
//array for options
var colorsArray =
[
["INSIGNIA", "Insignia"]
, ["ZINFANDE", "Zinfandel"]
, ["CARROT", "Carrot"]
, ["COFFEE", "Coffee"]
];
document.writeln(" <table cellspacing=\"0\" cellpadding=\"0\" id=\"product_colorsContainer\">");
for (var i=0; i<colorsArray.length; i++) {
add_color(i);
}
document.writeln(" </table>");
Firefox seems to accept whatever error I have, but IE7 just says error on line 170, which is a completely random section of code:document.writeln(" <label for=\"product_sizes\" class=\"required\"><strong>Size:</strong></label>");
I've been reading a little about DOM, but I was getting lost with adding nodes and tags and such. Also, if I have a DOM, what do I use to clear it out if I need to re-create it? I have another function function clear_colors() {
var colorsContainer = document.getElementById('product_colorsContainer');
var HTML = "";
colorsContainer.innerHTML = HTML;
}
Thanks.
//add_color will append a color option to the list
function add_color(colorIndex) {
var colorsContainer = document.getElementById('product_colorsContainer');
var HTML = colorsContainer.innerHTML;
HTML += " <tr>";
HTML += " <td>";
HTML += " <label for=\"product_color["+colorIndex+"]\">";
HTML += " <input type=\"radio\" value=\""+colorsArray[colorIndex][0]+"\" name=\"product_colors\" id=\"product_color["+colorIndex+"]\" onclick=\"javascript:setTimeout('update_sizes("+colorIndex+")', 50);\" /> "+colorsArray[colorIndex][1];
HTML += " </label>";
HTML += " </td>";
HTML += " </tr>";
colorsContainer.innerHTML = HTML;
}
//array for options
var colorsArray =
[
["INSIGNIA", "Insignia"]
, ["ZINFANDE", "Zinfandel"]
, ["CARROT", "Carrot"]
, ["COFFEE", "Coffee"]
];
document.writeln(" <table cellspacing=\"0\" cellpadding=\"0\" id=\"product_colorsContainer\">");
for (var i=0; i<colorsArray.length; i++) {
add_color(i);
}
document.writeln(" </table>");
Firefox seems to accept whatever error I have, but IE7 just says error on line 170, which is a completely random section of code:document.writeln(" <label for=\"product_sizes\" class=\"required\"><strong>Size:</strong></label>");
I've been reading a little about DOM, but I was getting lost with adding nodes and tags and such. Also, if I have a DOM, what do I use to clear it out if I need to re-create it? I have another function function clear_colors() {
var colorsContainer = document.getElementById('product_colorsContainer');
var HTML = "";
colorsContainer.innerHTML = HTML;
}
Thanks.