rhyspaterson Posted April 16, 2007 Share Posted April 16, 2007 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 Quote Link to comment Share on other sites More sharing options...
xenophobia Posted April 16, 2007 Share Posted April 16, 2007 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; } Quote Link to comment Share on other sites More sharing options...
paul2463 Posted April 16, 2007 Share Posted April 16, 2007 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] Quote Link to comment Share on other sites More sharing options...
fenway Posted April 20, 2007 Share Posted April 20, 2007 Sort of what like paul said... just pass an array of IDs, and have your function decide what to do to get at the elements. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.