Romani
Romani

Reputation: 3241

Retrieve all DOM elements in javascript

My application needs to retrieve all DOM elements in javascript and and put there values into one variable

i.e

if my application is having three text boxes then i want there values in comma separated form in javascript variable

Upvotes: 2

Views: 357

Answers (2)

Dr. Rajesh Rolen
Dr. Rajesh Rolen

Reputation: 14285

in below example i am taking values of all textboxes of a table. you can pass form name there.

var frm = document.getElementById('tblemail').getElementsByTagName("input");
var len = frm.length;
var myval='';
for (i=0;i< len;i++)
{
if (frm[i].type == "text")
{
if(myval =='')
{
myval = frm[i].value;
}else{
myval += ',' + frm[i].value;
}
}
}

Upvotes: 0

alex
alex

Reputation: 490213

If you want all DOM elements, which you probably don't, but if you do...

document.getElementsByTagName('*');

What I think you want is something like this

var form = document.getElementById('my-form').

var inputs = form.getElementsByTagName('input');

var inputsCsv = [];

for (var i = 0, length = inputs.length; i < length; i++) {

    if (inputs[i].type === 'text') {
        inputsCsv.push(inputs[i].value);
    }

}

inputsCsv = inputsCsv.join(',');

Upvotes: 2

Related Questions