JavaScript Using Javascript loop to create multiple HTML elements javascript using loop,in javascript loop,javascript in loop array,

Here's my answer. It's notated. In order to see the effects in the snippet you'll have to go into your developers console to either inspect the wrapper element or look at your developers console log.

We basically create some helper methods to easily create elements and append them to the DOM - it's really not as hard as it seems. This should also leave you in an easy place to append JSON retrieved Objects as properties to your elements!

Here's a Basic Version to give you the gist of what's happening and how to use it

//create element function

function create(tagName, props) {
  return Object.assign(document.createElement(tagName), (props || {}));
}

//append child function

function ac(p, c) {
  if (c) p.appendChild(c);
  return p;
}

//example: 
//get wrapper div
let mainWrapper = document.getElementById("mainWrapper");

//create link and div
let link = create("a", { href:"google.com" });
let div = create("div", { id: "myDiv" });

//add link as a child to div, add the result to mainWrapper
ac(mainWrapper, ac(div, link));

//create element function

function create(tagName, props) {
  return Object.assign(document.createElement(tagName), (props || {}));
}

//append child function

function ac(p, c) {
  if (c) p.appendChild(c);
  return p;
}

//example: 
//get wrapper div
let mainWrapper = document.getElementById("mainWrapper");

//create link and div
let link = create("a", { href:"google.com", textContent: "this text is a Link in the div" });
let div = create("div", { id: "myDiv", textContent: "this text is in the div! " });

//add link as a child to div, add the result to mainWrapper
ac(mainWrapper, ac(div, link));
div {
border: 3px solid black;
padding: 5px;

}
<div id="mainWrapper"></div>
Answer:1

Short version

Markup.js's loops.

Long version

You will find many solutions that work for this problem. But that may not be the point. The point is: is it right? And you may using the wrong tool for the problem.

I've worked with code that did similar things. I did not write it, but I had to work with it. You'll find that code like that quickly becomes very difficult to manage. You may think: "Oh, but I know what it's supposed to do. Once it's done, I won't change it."

Code falls into two categories:

  • Code you stop using and you therefore don't need to change.
  • Code you keep using and therefore that you will need to change.

So, "does it work?" is not the right question. There are many questions, but some of them are: "Will I be able to maintain this? Is it easy to read? If I change one part, does it only change the part I need to change or does it also change something else I don't mean to change?"

What I'm getting at here is that you should use a templating library. There are many for JavaScript.

In general, you should use a whole JavaScript application framework. There are three main ones nowadays:

  • ReactJS
  • Vue.js
  • Angular 2

For the sake of honesty, note I don't follow my own advice and still use Angular. (The original, not Angular 2.) But this is a steep learning curve. There are a lot of libraries that also include templating abilities.

But you've obviously got a whole project already set up and you want to just plug in a template into existing JavaScript code. You probably want a template language that does its thing and stays out of the way. When I started, I wanted that too. I used Markup.js . It's small, it's simple and it does what you want in this post.

https://github.com/adammark/Markup.js/

It's a first step. I think its loops feature are what you need. Start with that and work your way to a full framework in time.

Answer:2

I'm trying to read and add multiple files to an array. I've already found out that readAsArrayBuffer is a asynchronous function, so i need to wait for previous uploads to end. I tried to use callbacks ...

I'm trying to read and add multiple files to an array. I've already found out that readAsArrayBuffer is a asynchronous function, so i need to wait for previous uploads to end. I tried to use callbacks ...

  1. multiple file reading in java
  2. python multiple file reading
  3. reading multiple file in r

I am facing issues in writing into iframe, I am able to identify the iframe but I am not able to write anything into the frame. Whenever I try to write into the frame I am getting below error. org....

I am facing issues in writing into iframe, I am able to identify the iframe but I am not able to write anything into the frame. Whenever I try to write into the frame I am getting below error. org....

  1. need help writing a paper
  2. need help writing a book
  3. need help writing an essay
  4. need help writing a song
  5. need help writing a resume
  6. need help writing a letter
  7. need help writing a poem
  8. need help writing a cover letter
  9. need help writing vows
  10. need help writing my resume
  11. need help writing a sentence
  12. need help writing business plan
  13. need help writing rap lyrics
  14. need help writing a rap song
  15. need help writing my book
  16. need help writing a thank you note
  17. need help writing research paper
  18. need help writing my vows
  19. need help writing dissertation
  20. need help writing a love letter

I have a computed method: computed: { currentPosition () { if(this.get_local_storage_state()){ return this.lastLocation } if (this.currentRestaurant) { return this....

I have a computed method: computed: { currentPosition () { if(this.get_local_storage_state()){ return this.lastLocation } if (this.currentRestaurant) { return this....

This code form is working, but it doesn't call the ajax form chat_process.php. I need to call the ajax form and alert a message. var html = ""; for(...) { html += '<form class="...

This code form is working, but it doesn't call the ajax form chat_process.php. I need to call the ajax form and alert a message. var html = ""; for(...) { html += '<form class="...

  1. ajax form not working
  2. ajax form not working mvc
  3. ajax form submit not working
  4. ajax form serialize not working
  5. ajax form submit not working mvc
  6. ajax form preventdefault not working
  7. ajax form success not working
  8. ajax form post not working
  9. ajax form validation not working
  10. ajax form plugin not working
  11. ajax beginform not working
  12. ajax acf_form not working
  13. django ajax form not working