user3286692
user3286692

Reputation: 383

How to access property names that contain numbers within a loop?

I want to access values of below elments

opener.document.EditView.flight_no1_c.value
opener.document.EditView.flight_no2_c.value
opener.document.EditView.flight_no3_c.value
opener.document.EditView.flight_no4_c.value

Here only numbers are changing ranging from 1 to 4. How can I make this into loop.

Upvotes: 0

Views: 68

Answers (2)

Haim Raman
Haim Raman

Reputation: 12043

what about

for(var key in opener.document.EditView){
    if(key.match(/^flight_no\d+_c/)){
       console.log(key.value);
    }
}

Upvotes: 0

Girish
Girish

Reputation: 12127

You can use for loop and call properly using [] insteand on ., see below code

for(var i = 1; i <= 4; i++ ){
  opener.document.EditView["flight_no"+i+"_c"].value
}

Upvotes: 2

Related Questions