Jump to content

Mutiple element id's


rhyspaterson

Recommended Posts

Hey guys,

 

Just wondering if it is possible to return the value of multiple elements in one command? If that didn't make sense, which it probably doesn't because i'm an idiot, here is an example:

 

Original code is

 

var aText = document.getElementById('textarea1').value;

 

Which works nicely, but looking to return the value of three -

 

var aText = document.getElementById('textarea1', 'textarea2', 'textarea3').value;

 

Which doesn't work obviously. Just not sure how to do it, heh. Any help would be muchly appreciated! Thanks guys :)

Link to comment
Share on other sites

It actually depends on how you named your element's id. Issit in a sequence way? Eg:

textarea1

textarea2

textarea3

textarea4

.... so on....

 

If it is so, you can use loop to look through every elements:

var total_element = 10;

for(var i=1; i<total_element; i++){

    var aText = document.getElementById("textarea" + i).value;

}

Link to comment
Share on other sites

you could use the loop and place all the variables in a an array for use later on

 

var elementArray = new Array();
var total_element = 10;
for(var i=1; i<total_element; i++)
{
    elementArray[i-1]= document.getElementById("textarea" + i).value;
}

this is written this way because array all start at 0 --> elementArray[i-1]

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.