Information sur les inputs de formulaire

Contenu du snippet

Je me sers souvent de ce code dans un fichier JS.
Il me permet d'avoir rapidement des informations sur les differents INPUTs. Les pages que je developpe sont souvent dynamique et je modifie souvent les elements directement avec du JavaScript. Donc en peu de ligne, j'ai acces aux types, aux nombres, aux options des INPUTs de la page. Je peux savoir lequels sont "checked" ou lequels sont "disable", etc...

Source / Exemple :


// ***********************************************
function inputInfo(obj, query, debug)
{
/***********************************************
    The argument 'query' is optional (see below)
        ex: info = inputInfo(Form.EXCH, "value");
    Without the argument 'query', you return a Array with all this values: (see below)
    You can take this value with a period or an array indicator: 
        ex: info = inputInfo(Form.EXCH);  
            Type_Of_Input = info.type;
        OR 
            Type_Of_Input = info.[0];
    If "obj" is a radio or checkbox type:
         obj.value return the value for the CHECKED element or return null if none are CHECKED.
         obj.select return true if one or more is CHECKED.

    Possible value for the query's argument
        info[0] = info.type
        info[1] = info.name
        info[2] = info.len
        info[3] = info.value
        info[4] = info.select
        info[5] = info.enabled
        info[6] = info.multiple
        info[7] = info.text
        info[8] = info.nbOpt (nombre d'elements OPTION dans le SELECT)

                                                                                              • /
var tmpResult = new Array(); var info = new Array(); if (obj) { if (obj.type) { info[0] = info.type = obj.type; info[1] = info.name = obj.name; info[2] = info.len = false; if (obj.type == "select-one") { tmpResult = SelectOne (obj); info[3] = info.value = tmpResult[0]; info[4] = info.select = tmpResult[1]; info[7] = info.text = tmpResult[2]; info[8] = info.nbOpt = tmpResult[3]; } else if ((obj.type == "radio") || (obj.type == "checkbox")) { info[3] = info.value = (obj.checked)? obj.value : null; info[4] = info.select = (obj.checked)? true : false; } else info[3] = info.value = obj.value; info[5] = info.enabled = (obj.disabled)?false:true; info[6] = info.multiple = false; } else if (obj.length) { var tmp_type = new Array(); var tmp_name = new Array(); var tmp_len = new Array(); var tmp_value = new Array(); var tmp_select = new Array(); var tmp_enabled = new Array(); var tmp_text = new Array(); var tmp_nbOpt = new Array(); var flag = true; for (Y = 0; Y < obj.length; Y++) { tmpResult = inputInfo(obj[Y]) tmp_type[Y] = tmpResult[0]; tmp_name[Y] = tmpResult[1]; tmp_value[Y] = tmpResult[3]; tmp_select[Y] = tmpResult[4]; tmp_enabled[Y] = tmpResult[5]; tmp_text[Y] = tmpResult[7]; tmp_nbOpt[Y] = tmpResult[8]; if (flag && obj[0].type != tmp_type[Y]) flag = false; } info[0] = info.type = (flag)? obj[0].type : tmp_type; info[1] = info.name = obj[0].name; info[2] = info.len = obj.length; info[3] = info.value = (flag && info.type == "radio")? tmp_value.toString().match(/\w+/) : tmp_value; // match is for return the unique Array value in the selected radio input info[4] = info.select = tmp_select; info[5] = info.enabled = tmp_enabled; info[6] = info.multiple = true; if (tmp_text) info[7] = info.text = tmp_text; if (tmp_nbOpt) info[8] = info.nbOpt = tmp_nbOpt; } if (debug) { var msg = "info.type = " + info[0] + "\n"; msg += "info.name = " + info[1] + "\n"; msg += "info.len = " + info[2] + "\n"; msg += "info.value = " + info[3] + "\n"; msg += "info.select = " + info[4] + "\n"; msg += "info.enabled = " + info[5] + "\n"; msg += "info.multiple = " + info[6] + "\n"; msg += "info.text = " + info[7] + "\n"; msg += "info.nbOpt = " + info[8] + "\n"; alert (msg); } return ((query) ? info[query] : info); } return false; } // *********************************************** function ToString(val) { return val.toString(); } // *********************************************** function SelectOne(obj) { var info = new Array(); if (obj.selectedIndex == -1 ) { info[1] = false; info[0] = ""; info[2] = ""; } else { info[1] = ToString (obj.selectedIndex); info[0] = obj.options[obj.selectedIndex].value; info[2] = obj.options[obj.selectedIndex].text; } info[3] = obj.length; return info; }

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.