# \[SOLVED\] javascript - getElementById

**URL:** <https://onehack.st/t/solved-javascript-getelementbyid/75181>\
**Category:** Discussion & Solutions\
**Tags:** solved, programming\
**Created:** [March 25, 2020, 8:22pm UTC](https://onehack.st/t/solved-javascript-getelementbyid/75181 "2020-03-25T20:22:21Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rijo\_Timko](https://onehack.st/user_avatar/onehack.st/rijo_timko/32/15818_2.png) [@Rijo\_Timko](https://onehack.st/u/Rijo_Timko)\
**Post date:** [March 25, 2020, 8:22pm UTC](https://onehack.st/t/solved-javascript-getelementbyid/75181/1 "2020-03-25T20:22:22Z")

</div>

I need help from a javascript developer.  
I’m creating many dynamical input elements using js function appendChild. When i want to get id by using function getElementById, it doesnt work. What am I doing wrong?

---

<div class="post-metadata">

**Author:** ![najib\_mayada](https://onehack.st/user_avatar/onehack.st/najib_mayada/32/28119_2.png) [@najib\_mayada](https://onehack.st/u/najib_mayada)\
**Post date:** [March 25, 2020, 8:43pm UTC](https://onehack.st/t/solved-javascript-getelementbyid/75181/2 "2020-03-25T20:43:35Z")

</div>

please share your code so we can understand more

---

<div class="post-metadata">

**Author:** ![ahmedayman4a](https://onehack.st/user_avatar/onehack.st/ahmedayman4a/32/37430_2.png) [@ahmedayman4a](https://onehack.st/u/ahmedayman4a)\
**Post date:** [March 25, 2020, 8:59pm UTC](https://onehack.st/t/solved-javascript-getelementbyid/75181/3 "2020-03-25T20:59:58Z")

</div>

make sure you are setting the id when creating the element like that :

```
var myDiv = document.createElement("div");
myDiv.id = "article-1";
var parent = document.getElementsByTagName("body")[0];
parent.append(myDiv);
........
var myCreatedDiv = document.getElementById("article-1");

```

---

<div class="post-metadata">

**Author:** ![Star\_Lite](https://onehack.st/user_avatar/onehack.st/star_lite/32/18340_2.png) [@Star\_Lite](https://onehack.st/u/Star_Lite)\
**Post date:** [March 26, 2020, 5:17am UTC](https://onehack.st/t/solved-javascript-getelementbyid/75181/4 "2020-03-26T05:17:33Z")

</div>

First of all its really hard to help without seeing any code, second you need to select the element you are trying to append to then append the child elements, third getElementById is meant to get only one element by id if you are trying to select multi elements use document.querySelectorAll.

---

<div class="post-metadata">

**Author:** ![Aina](https://onehack.st/user_avatar/onehack.st/aina/32/167469_2.png) [@Aina](https://onehack.st/u/Aina)\
**Post date:** [March 26, 2020, 5:22am UTC](https://onehack.st/t/solved-javascript-getelementbyid/75181/5 "2020-03-26T05:22:50Z")

</div>

> [@\#FAQ - Getting Started in 1Hack](https://onehack.st/t/faq-getting-started-in-1hack/5/1):
>
> # What Kind of “help” Queries Are Not Allowed?
> 
> We do not allow anyone asking for the legal stuff illegally!
> 
> **Some examples!**
> 
> > - Asking legal content Illegally or asking any other illegal content that can hurt any brand or individual’s royalty is strictly prohibited from posting! ⚠
> > - I want this course/tutorial/books, please anyone can give it to me, in case you guys have it? 🤔
> > - How to get the “xyz brand/company’s” free account or premium account for free? 😃
> > - How can I earn money easily, rapidly, smoothly, efficiently… fast, etc? 🤦‍♀️
> > - I need this tool/app how to get this free/premium tool/app, avoid asking to get things this way…
> > - How to code this, can you help me with a coding script? I am stuck with a code, now what to do? \<\<\< Please, avoid asking such deep programming language questions, as there is already a website dedicated for this **stackoverflow**! 🙁

## Read the FAQ before asking any question.

## search at google and follow resources to contact any coder. Good luck!

---

<div class="post-metadata">

**Author:** ![Rijo\_Timko](https://onehack.st/user_avatar/onehack.st/rijo_timko/32/15818_2.png) [@Rijo\_Timko](https://onehack.st/u/Rijo_Timko)\
**Post date:** [March 26, 2020, 9:03am UTC](https://onehack.st/t/solved-javascript-getelementbyid/75181/6 "2020-03-26T09:03:55Z")

</div>

I have solved problem using two functions. One for creating elements by createElement and appendChild.  
The second function I used getElementById. I got the id dinamically. I used loop thats look like …‘id’+i .  
Sorry for bad English and sorry if I had broken some rules of the [Onehack.us](http://Onehack.us).
