/    Sign up×
Community /Pin to ProfileBookmark

Get the value from an array of elements

Hi I want to loop through an array of elements and get the value from each element. How can I do that?

I have tried with the code below but it doesn´t work? Can you help me?

for ( i = 0 ; i < countElements ; i++ ){

alert( document.form1.getElementById(‘test’)[i].value );
}

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@mrhooJul 30.2006 — .getElementById('test')[i] is not an element of an array,

but a property i of the element with the id 'test'-

chances are the value is undefined.
Copy linkTweet thisAlerts:
@JUDJul 30.2006 — It depends what type of elements you need to loop through.

Lets say you wanted to get the values of all the textbox input tags on the page. You could do that like this.

[CODE]
var countTags = document.getElementsByTagName("INPUT");

for(var i = 0; i < countTags.length; i++){
if(countTags[i].type == 'text'){
alert(countTags[i].value);
}
}
[/CODE]
Copy linkTweet thisAlerts:
@jeppeauthorJul 31.2006 — Hi,

The problem is that I have other text elements that I don´t want to loop through?
Copy linkTweet thisAlerts:
@KorJul 31.2006 — 1.[B] id[/B] must be unique on page/session, so that you must not have more than 1 id with the same value on the page. getElementById() returns, thus, a single element, not a collection

  • 2. It is not the problem if you want or not to loop through elements, it's about the possible ways to loop, and those possibilities are limited


  • - the collection of all the elements of a form

    document.forms[[I]name_or_index[/I]].elements[[I]name_or_index[/I]]

  • - the collection of all the HTML (tags) elements in a document

    document.getElementsByTagName('*')

  • - the collection of all the elements with the same tagname

    document.getElementsByTagName([I]tagname[/I])

  • - the collection of all the elements with the same name

    document.getElementsByName([I]name[/I])




  • To restrict the loop, you may use the fact that the DOM reference can be concatenated.

    document.getElementById([I]id[/I]).getElementsByTagName([I]tagname[/I]);

    ex: the collection of a table's rows

    document.getElementById('tableid').getElementsByTagName('tr');
    ×

    Success!

    Help @jeppe spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 5.6,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,

    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,
    )...