If I dynamically create a form field with a reference to a JS object in an expando property, what would be the best way to add it to the page? Obviously I can't use document.write() because the JS object isn't a string. Right now I'm creating a temporary SPAN object, adding my custom element, then destroying the SPAN.
Is there a better way than the writeElement() function I made?
Adam
There are only 10 types of people in the world: Those who understand binary, and those who don't
Is there a better way than the writeElement() function I made?
Code:
<html>
<head>
<script>
PhoneObject = function(id, label, title, defaultValue)
{
this.ui = document.createElement("INPUT");
this.ui.type = 'text';
this.ui.id = id;
this.ui.name = id;
this.ui.defaultValue = defaultValue || '';
this.ui.label = label || id;
this.ui.title = title || label || id;
this.ui.obj = this;
this.ui.maxlength = '10';
this.ui.onblur = function()
{
this.obj.validate();
}
this.validate = function()
{
var v = this.ui.value.replace(/[^0-9]+/gi,"");
if(v.length==10)
{
this.ui.value='('+v.substr(0,3)+')'+v.substr(3,3)+'-'+v.substr(6,4);
}
else
{
return this.ui.label + " must consist of ten digits.";
}
}
this.render = function()
{
writeElement(this.ui);
}
}
[red]// Is creating a temp SPAN the best way?
function writeElement(obj)
{
document.write('<span id="spnTemp"></span>');
var spnTemp = document.getElementById("spnTemp");
spnTemp.parentNode.insertBefore(obj,spnTemp);
spnTemp.parentNode.removeChild(spnTemp);
}[/red]
</script>
</head>
<body>
<form>
<script>
(new PhoneObject('HomePhone','Home Phone','Please enter your home phone number')).render();
</script>
</form>
</body>
</html>
Adam
There are only 10 types of people in the world: Those who understand binary, and those who don't