# Use function in multiple divs?

**URL:** <https://www.phphelp.com/t/use-function-in-multiple-divs/34443>\
**Category:** Javascript & Ajax\
**Created:** [January 23, 2022, 2:38pm UTC](https://www.phphelp.com/t/use-function-in-multiple-divs/34443 "2022-01-23T14:38:30Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Anette](https://www.phphelp.com/letter_avatar_proxy/v4/letter/a/e9bcb4/32.png) [@Anette](https://www.phphelp.com/u/Anette)\
**Post date:** [January 23, 2022, 2:38pm UTC](https://www.phphelp.com/t/use-function-in-multiple-divs/34443/1 "2022-01-23T14:38:30Z")

</div>

If I have four div’s with id 1,2,3,4 and I want this function to write “good morning” in each div.  
My code below does not work.  
Does anyone know?

```auto
function beCheerful(x) {
	let line = "";
	var box = document.getElementById(x);
	line +=('good morning ');
	box.innerHTML = line;
}
		
for(x = 1; x <=4; x++){
   beCheerful(x);
}

```

---

<div class="post-metadata">

**Author:** ![phdr](https://www.phphelp.com/letter_avatar_proxy/v4/letter/p/91b2a8/32.png) [@phdr](https://www.phphelp.com/u/phdr)\
**Post date:** [January 23, 2022, 5:35pm UTC](https://www.phphelp.com/t/use-function-in-multiple-divs/34443/2 "2022-01-23T17:35:41Z")

</div>

The posted code works for me. Therefore, the problem is elsewhere, such as in the order of the code on the page or some other error in the markup/javascript. Have you checked the browser’s develop tools console tab?

---

<div class="post-metadata">

**Author:** ![Anette](https://www.phphelp.com/letter_avatar_proxy/v4/letter/a/e9bcb4/32.png) [@Anette](https://www.phphelp.com/u/Anette)\
**Post date:** [January 24, 2022, 7:36am UTC](https://www.phphelp.com/t/use-function-in-multiple-divs/34443/3 "2022-01-24T07:36:23Z")

</div>

Ok, thanks! I got it to work.  
But only with the code on the same page.  
How can I get it to work it in a separat .js file?

---

<div class="post-metadata">

**Author:** ![skawid](https://www.phphelp.com/letter_avatar_proxy/v4/letter/s/bbe5ce/32.png) [@skawid](https://www.phphelp.com/u/skawid)\
**Post date:** [January 24, 2022, 7:55am UTC](https://www.phphelp.com/t/use-function-in-multiple-divs/34443/4 "2022-01-24T07:55:44Z")

</div>

Move your javascript to a separate file, usually named with a `.js` extension - eg `hellodivs.js`.

Then you can use the [`<script>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script#examples) tag to include that file in your page:

```auto
<script src="hellodivs.js"></script>

```

The `<script>` tag won’t show any content in the browser; it’s just a signal to load the associated javascript. It’s usually best to add the `<script>` tag just before the closing `</body>` tag, to ensure the browser knows you html structure before it tries to run your code against it.

---

<div class="post-metadata">

**Author:** ![Anette](https://www.phphelp.com/letter_avatar_proxy/v4/letter/a/e9bcb4/32.png) [@Anette](https://www.phphelp.com/u/Anette)\
**Post date:** [January 24, 2022, 8:18am UTC](https://www.phphelp.com/t/use-function-in-multiple-divs/34443/5 "2022-01-24T08:18:21Z")

</div>

That’s what I tried from the beginning, but it doesent work from separat file!

---

<div class="post-metadata">

**Author:** ![skawid](https://www.phphelp.com/letter_avatar_proxy/v4/letter/s/bbe5ce/32.png) [@skawid](https://www.phphelp.com/u/skawid)\
**Post date:** [January 24, 2022, 12:05pm UTC](https://www.phphelp.com/t/use-function-in-multiple-divs/34443/6 "2022-01-24T12:05:24Z")

</div>

Could you show us the contents of your HTML and JS file?
