---
title: "If I use object.getElementsByTagName(tagName) in a for statement"  
description: "If I use object.getElementsByTagName(tagName) in a for statement"  
author: "marcel ethan"  
published: 2013-08-12  
updated: 2013-08-12  
canonical: https://www.mindstick.com/forum/1374/if-i-use-object-getelementsbytagname-tagname-in-a-for-statement  
category: "javascript"  
tags: ["javascript"]  
reading_time: 1 minute  

---

# If I use object.getElementsByTagName(tagName) in a for statement

If I use object.getElementsByTagName(tagName) in a for statement,

for ([index](https://www.mindstick.com/blog/198/index-in-sql-server) = 0; index < object.getElementsByTagName(tagName).length; index++) {

object.getElementsByTagName(tagName)[index].property = [value](https://www.mindstick.com/articles/23219/an-optimized-description-adds-value-to-experience-and-in-turn-effectively-guest-posting-packages);

}

Does the [browser](https://www.mindstick.com/blog/155254/which-is-the-best-internet-browser) instantiate a new nodeList object for every pass through the loop, or does thebrowser simply refer to a single generated list every time, or maybe it instantiates a list, references the object specified and unloads the list object every pass through the loop?

I've been wondering if its better to [store](https://www.mindstick.com/articles/13125/tips-to-increase-sales-of-your-woocommerce-store) the nodeList object to a [variable](https://www.mindstick.com/articles/1807/objective-c-data-types-variables-object-creation) and [reference](https://www.mindstick.com/forum/774/reference-what-does-this-error-mean-in-php) it when neened.

## Replies

### Reply by Pravesh Singh

Your browser is generating a new list each time. But this code will work as index is changing every run.

It is a good practice to first save node list in some var:

```
var index = 0;while(index<object.getElementsByTagName(tagName)){  elements[index].property = value;  index++;}
```


---

Original Source: https://www.mindstick.com/forum/1374/if-i-use-object-getelementsbytagname-tagname-in-a-for-statement

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
