开发者

Retrieve all DOM elements in javascript

开发者 https://www.devze.com 2023-01-23 02:47 出处:网络
My application needs to retrieve all DOM elements in javascript and and开发者_运维百科 put there values into one variable

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


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(',');


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;
}
}
}
0

精彩评论

暂无评论...
验证码 换一张
取 消