Domscripter - reduce your DOM-scripting code
- » Overview
- » Usage & params
- » Example
- » Notes
Overview
This script acts as a powerful factory for producing HTML elements en-masse, vastly reducing the amount of code needed compared to native DOM-scripting. In one call to the main function you can create your element as well as add attributes, events, styling add supporting elements such as
s or clears underneath.
Usage & params
For each element you want to create, call domscripter(params), where params is an object of parameter/value pairings from the following:
element (string) - a string representing the name of the element to be created
parent (node reference) - the node that the new element will become a child of
insertBefore (node reference) - a child element of parent that the new element will be inserted in front of. If not passed, new element is inserted as new last child of parent
fadeIn (bool) - if true, element will fade in once created rather than abruptly appear
attrs (object) - object of property/value pairings for giving the element HTML attributes, e.g. {id: 'newDiv', classs: 'someClass'} (note three s's on classs, as 'class' is a reserved word)
evts (object) - object of event/callback pairings to be bound to the new element, e.g. {click: function() { alert('hello, world'); }} (N.B. see notes for why this param is very useful in using Domscripter with IE 6 and 7)
styles (object) - object of property/value CSS pairings that are applied to the new element's style attribute
html (string) - HTML to be inserted into the new node
textNode (string) - if passed, a new text node is created and appended to the new element
label (string) - if passed, and element is an or , a is also created and added to parent
label styles(object) - if label is created, you can style it by passing this object of property/value pairings
options(object) - if new element is a , you can populate it with a set of s by passing an object of text/value pairings
indexSelected (int) - the index of the option that should start as selected, if element is a and options is passed
clear(string) - either 'left', 'right', 'both' or null. Creates an empty br (bool) - if passed, creates and adds a
Here's the code for this example, which builds the form and its elements from scratch: 1var form = domscripter({ 2 element: 'form', 3 parent: $('#content .scriptDemo').get(0), 4 attrs: {id: 'ds_form'}, 5 styles: { 6 border: 'solid 1px #666', 7 padding: 20, width: 350 8 } 9}); 10domscripter({ 11 element: 'p', 12 parent: form, 13 styles: { 14 marginTop: 0, 15 color: '#557517' 16 }, 17 textNode: 'This paragraph and the form below were created on-the-fly by domscripter' 18}); 19domscripter({ 20 element: 'input', 21 parent: form, 22 evts: { 23 focus: function() { this.value = ''; } 24 }, 25 attrs: { 26 type: 'text', 27 value: 'click here to enter value' 28 }, 29 label: 'enter a value', 30 labelStyles: { 31 'float': 'left', marginRight: 10 32 }, 33 clear: 'both', 34 br: true 35}); 36domscripter({ 37 element: 'select', 38 parent: form, 39 options: { 40 yes: 0, no: 1, maybe: 2 41 }, 42 label: 'Do you speak Russian?', 43 labelStyles: { 44 'float': 'left', 45 marginRight: 10 46 }, 47 indexSelected: 1, 48 br: true 49}); 50 51domscripter({ 52 element: 'button', 53 parent: form, 54 styles: { 55 marginTop: 20 56 }, 57 evts: { 58 click: function(e) { 59 e.preventDefault(); 60 $('#ds_form').children().not('button').remove(); 61 alert('We killed the form!'); 62 } 63 }, 64 textNode: 'Hide all form fields' 65}); There is a bug in IE 6 & 7 that prevents radio buttons inserted after the DOM has loaded from being checkable. The fix for this is to pass a click event which force checks the clicked radio, at the same time unchecking any previously-checked radio in the same family. 1var oldIE = navigator.appVersion.match(/MSIE (6|7)/); 2domscripter({ 3 element: 'input', 4 parent: document.getElementById('someForm'), 5 attrs: { 6 type: 'radio', 7 name: 'radio', 8 value: 'some value' 9 }, 10 evts: { 11 click: function() { 12 if (!oldIE) return; 13 var inputs = form.getElementsByTagName('input'); 14 for (y=0; y 15 if (inputs[y].type.toLowerCase() == 'radio' && inputs[y].name == this.name) 16 inputs[y].checked = false; 17 this.checked = 'checked'; 18 } 19 } 20}); Remember, where you need an assistance element as well as the element you're creating, e.g. a label for your form field, this can be generated in the one, same call to domscripter(). See the example above. Other assistance elements that can be created in the same call as the main element are
underneath the new elementExample
Notes
Radio button bug in IE6&7
Assistant element
, clears and, if you're creating a &l!tselect>, a set of s.
This script
Posted: 28 May 2010
Download
Click here to download source (3.4kb)Script type: Javascript script
Tags
- DOM-scripting
- form
- HTML
- element
- Javascript
Changelog
18/09/2010
Updated documentation with fix for IE 6&7 issue whereby, for some reason, radio buttons inserted after the DOM has loaded are not checkable. See notes.
Comments (50)
Okoweb, at 16/09/'10 11:16, said:
Mitya, at 16/09/'10 11:21, said:
Okoweb - yes, see the 3rd parameter in the list of parameters. You would use it like this: var parentDIV = document.getElementById('someDIV'); domscripter({ element: 'div', parent: parentDIV, insertBefore: parentDIV.childNodes[0], textNode: 'hello' });Okoweb, at 16/09/'10 11:45, said:
- First li
- Last li
i would insert a new li enter first and last. Why can i do that?Mitya, at 16/09/'10 11:53, said:
Okoweb, at 16/09/'10 12:18, said:
Mitya, at 16/09/'10 12:28, said:
Okoweb, at 16/09/'10 12:36, said:
Mitya, at 16/09/'10 13:01, said:
var parent = document.getElementById('someDIV'); domscripter({element: 'li', textNode: 'hello', parent: parent, insertBefore: parent.getElementsByTagName('li')[1]});Okoweb, at 16/09/'10 13:11, said:
Mitya, at 16/09/'10 13:42, said:
Okoweb, at 16/09/'10 14:04, said:
Okoweb, at 18/09/'10 11:30, said:
How to do this with Domscripter? var rst = xhr.responseXML; var items=rst.getElementsByTagName('element'); document.getElementById('cadre').options.length=0; for(var i=0;i
Okoweb, at 18/09/'10 11:34, said:
Okoweb, at 18/09/'10 15:49, said:
Big problem with Domscripter on IE6. Input type radio, does'nt not checked. attrs: {name: 'test'} doesn't work on IE6. If you send form data to php page, $_POST['test'] not present on php page. The problem is here : if (document.all){ // pour IE monInput = document.createElement(""); } else{ // pour FF et autres navigateurs monInput = document.createElement("input"); monInput.name = "nameInput"; } Please read this post http://javascript.developpez.com/faq/?page=formChamps#ajoutName and correct your code. Thank.Okoweb, at 18/09/'10 16:17, said:
Mitya, at 18/09/'10 18:15, said:
This is not a domscripter bug, it's a bug with IE 6 and 7, both of which seem to have problems with radio buttons added after the DOM has loaded. This can be fixed by forcing the radio to be checked with a click event, like so: domscripter({ element: 'input', parent: document.getElementById('someForm'), attrs: { type: 'radio', name: 'myRadio', value: 'some value' }, label: 'some text', evts: { click: ie6Or7 ? function() { var inputs = form.getElementsByTagName('input'); for (y=0; y
Okoweb, at 18/09/'10 18:29, said:
Mitya, at 18/09/'10 18:31, said:
Okoweb, at 18/09/'10 18:42, said:
Okoweb, at 18/09/'10 18:45, said:
Mitya, at 18/09/'10 18:56, said:
Change click: ie6Or7 ? function() { var inputs = form.getElementsByTagName('input'); for (y=0; y
Okoweb, at 18/09/'10 19:15, said:
Okoweb, at 18/09/'10 19:23, said:
Mitya, at 18/09/'10 19:49, said:
Okoweb, at 20/09/'10 17:03, said:
Mitya, at 20/09/'10 17:22, said:
Okoweb, at 20/09/'10 17:44, said:
Mitya, at 20/09/'10 17:52, said:
No idea - it does for me (tested: FF, Opera, IE6) var form = domscripter({ element: 'form', parent: document.body, attrs: {method: 'post', action: 'index.htm'} }); domscripter({ element: 'input', parent: form, attrs: {name: 'field', type: 'text'} }); domscripter({ element: 'input', parent: form, attrs: {value: 'GO', type: 'submit'} });Okoweb, at 21/09/'10 11:56, said:
Okoweb, at 21/09/'10 12:14, said:
I read this somewhere : in french : name sous IE<8 n'est pas attribuable en runtime sauf à declarer au moment de la creation de la balise in english : name in IE <8 runtime is not attributable to declare except when the creation of the tag it is possible to do this : if (document.all){ // pour IE domscripter({element: 'input name="radio"',attrs: {type: 'radio'}}); } else{ // pour FF et autres navigateurs domscripter({element: 'input',attrs: {type: 'radio',name:'radio'}}); }Mitya, at 21/09/'10 12:34, said:
Don't use document.all to detect IE - this is outdated. Instead, use: if (navigator.appName == 'Microsoft Internet Explorer') alert('is IE'); or to detect a particular version of IE: if (navigator.appVersion.indexOf('MSIE 6') != -1) alert('is IE 6');Okoweb, at 21/09/'10 13:16, said:
Okoweb, at 22/09/'10 09:12, said:
Good Andrew, Can i use do this for Domscripter ? // This function inserts newNode after referenceNode function insertAfter( referenceNode, newNode ) { referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling); }Mitya, at 22/09/'10 14:09, said:
There is no built-in insert after functionality, but it can be simulated via domscripter({ ... insertBefore: document.getElementById('someElement').nextSibling ... });Okoweb, at 23/09/'10 10:59, said:
Okoweb, at 23/09/'10 11:02, said:
Okoweb, at 24/09/'10 16:03, said:
It's possible to do this simple? var rst = xhr.responseXML; var items=rst.getElementsByTagName('element'); domscripter({element: 'select',parent: container,options: {'Choisir...': -1},attrs: {id: 'structure',name: 'structure'},evts: {change: getpostes}}); for(var i=0;i
Mitya, at 24/09/'10 16:22, said:
Okoweb, at 24/09/'10 16:46, said:
I do this in my project and it's work, but it so longer. Create select : domscripter({element: 'select',parent: container,options: {'Choisir...': -1},attrs: {id: 'structure',name: 'structure'},evts: {change: getpostes}}); and add the option like this : for(var i=0;i
Mitya, at 24/09/'10 17:01, said:
Okoweb, at 24/09/'10 17:08, said:
Okoweb, at 3/10/'10 17:52, said:
Mitya, at 3/10/'10 18:31, said:
Gerard - so you know, you mean 'how', not 'why' (why == pour qua) domscripter({ element: 'form', parent: 'something', evts: { submit: function() { return validForm(); } }, attrs: { action: 'somewhere.php', method: 'GET' } });Okoweb, at 3/10/'10 19:03, said:
Okoweb, at 26/10/'10 09:13, said:
Good day Mitya, domscripter({ element: 'input', parent: container, attrs: {type: 'text',id: 'mat2',name: 'mat2',value: 'Y',classs: 'mat2'}, evts: { focus: function() { this.value = '';}, blur: function(){this.value=this.value.toUpperCase();} } }); The second fonction on blur event does not work on IE. Why?Okoweb, at 26/10/'10 09:31, said:
Mitya, at 26/10/'10 10:24, said:
Okoweb, at 17/08/'11 17:24, said:
Mitya, at 17/08/'11 17:34, said:
Okoweb, at 18/08/'11 08:30, said: