Home & blog  /  Scripts  /  view post  /

Domscripter - reduce your DOM-scripting code

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 , 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 "); } 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:
I create a post on here http://www.developpez.net/forums/d976842/webmasters-developpement-web/javascript/attribuer-lattribut-name-element-cree-domscripter/ and the demo of the problem is here 

Please try it on IE 6 and see the bug checked="checked" not work.

Thank.
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:
Thank for your response, but why this code doesn't  work?
See here 
Mitya, at 18/09/'10 18:31, said:
Sorry, I forgot to give you the following line. Put it anywhere in your code before the call to domscripter

ie6Or7 = navigator.appVersion.match(/MSIE (6|7)/);
Okoweb, at 18/09/'10 18:42, said:
Try again with IE6, checked doesn't work. Firefox doesn't work.
Okoweb, at 18/09/'10 18:45, said:
Try again this link 
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:
Try again on IE6, dosen't work. View my source code.
Okoweb, at 18/09/'10 19:23, said:
My source code.
Mitya, at 18/09/'10 19:49, said:
Works for me - see:

https://mitya.co.uk/scripts/demo/ie6&7_fix_demo.htm
Okoweb, at 20/09/'10 17:03, said:
Your link for the demo doesn't work. Thank.
Mitya, at 20/09/'10 17:22, said:
It does - it's just a problem with my comments system. Don't click it - copy and paste it into your browser instead.
Okoweb, at 20/09/'10 17:44, said:
The attribut method for my form is 'POST', but the data was trasmitted on GET Method why?

Thank!
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:
I do this and my script run well.


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:
Thank
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:
Good, thank !
Okoweb, at 23/09/'10 11:02, said:
Why can i simulate the replaceChild method.
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:
What's the question?
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:
Sorry, without delving into your coding context I can't really help.

Domscripter is just a node generator. You can work with the nodes it returns just as you would with nodes you make with native JS, without Domscripter.
Okoweb, at 24/09/'10 17:08, said:
Thank.
Okoweb, at 3/10/'10 17:52, said:
Why can i do that with your code :
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:
Thank !
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:


This code work why the first does'nt work?

Thank
Mitya, at 26/10/'10 10:24, said:
I don't know. I have had issues before with IE and the blur event. Domscripter registers the blur event properly - if it's not firing, I don't know why.
Okoweb, at 17/08/'11 17:24, said:
Good day,
why this code dos'nt work on Firefox 3. See here 

Thank for your work.
Mitya, at 17/08/'11 17:34, said:
Okoweb - you have JS errors in your code. Please eliminate these first. Check the error console.
Okoweb, at 18/08/'11 08:30, said:
Resolved.
Thank.
post new comment