Willy Duitt
11-22-2004, 01:34 AM
You are not seeing the problem for what it is...
Forget about "Add Another Set"... your Add Option is not OOP and thus does not release its previous reference.... Think about what it is you are trying to pass... set, option.... or both....
shane956
11-22-2004, 02:55 AM
Thanks for the reply,
Like I said, my javascript is really weak. I'll have to go through some OO javascript tutorials before I can go about this.
hemebond
11-22-2004, 03:25 AM
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<title>47806</title>
</head>
<body>
<form id="form" name="form1">
<input id="add_set_button" type="button" value="add option set" onclick="addSet()">
</form>
<script type="text/javascript">
var MAX_OPTIONS = 20;
var form = document.getElementById("form");
function addSet()
{
var sets = document.getElementsByName("set");
var fieldset = document.createElement("fieldset");
fieldset.setAttribute('id', sets.length);
fieldset.setAttribute('name', 'set');
fieldset.appendChild( createLegend('Option set ' + sets.length) );
fieldset.appendChild( createLabel('name', 'set_name[' + sets.length + ']') );
fieldset.appendChild( createInput('set_name[' + sets.length + ']', 'text', '', '41') );
fieldset.appendChild( createButton('add option', 'onclick', 'addOption(this.parentNode)') );
form.insertBefore(fieldset, document.getElementById("add_set_button"));
}
function addOption(set)
{
var options = set.getElementsByTagName("fieldset");
if(options.length >= MAX_OPTIONS)
{
return;
}
var fieldset = document.createElement("fieldset");
fieldset.appendChild( createLegend("Option " + options.length) );
fieldset.appendChild( createLabel('name', 'name[' + set.id + '][' + options.length + ']'));
fieldset.appendChild( createInput('name[' + set.id + '][' + options.length + ']', 'text', '', 15));
fieldset.appendChild( createLabel('price $', 'price[' + set.id + '][' + options.length + ']'));
fieldset.appendChild( createInput('price[' + set.id + '][' + options.length + ']', 'text', '0.00', 6));
set.insertBefore(fieldset, set.lastChild);
}
function createLegend(text)
{
var legend = document.createElement("legend");
text = document.createTextNode(text);
legend.appendChild(text);
return legend;
}
function createLabel(text,element)
{
var label = document.createElement("label");
label.appendChild(document.createTextNode(text));
label.setAttribute("for",element);
return label;
}
function createInput(id, type, value, size)
{
var input = document.createElement("input");
input.setAttribute("id",id);
input.setAttribute("name",id);
input.setAttribute("type",type);
input.setAttribute("value",value);
input.setAttribute("size",size);
return input;
}
function createButton(value, event, action)
{
var button = document.createElement("input");
button.setAttribute("type","button");
button.setAttribute("value",value);
button.setAttribute(event, action);
return button;
}
</script>
</body>
</html>Ideally this should be split into 2 different pages. Page 1 allows you to add a new set, and page 2 allows you to add options to this set.
Oh, and this doesn't work in Internet Explorer because IE does not support the getElementsByName method, contrary to their claims.
shane956
11-22-2004, 08:18 PM
Thank you very much for this. It's exactly what I was aiming for. The only thing is that the application is to be run under IE. Is there no way to have this work under IE? Cross-browser compatibility isn't an issue for me here since I only need to work under IE.
Shane