Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Thursday, February 4, 2016

Add to element as divs

Add to element as divs


Okay, so here's my code: HTML:

 

Header

Results 1

JS:

 (function() {       var results = document.getElementById('results1');       var drink = [ 'Rum', 'Vodka', 'Whiskey', 'Beer' ];   })();  

I need to add the drinks to the results element as divs.

Answer by shennan for Add to element as divs


With pure JavaScript:

(function() {      var results = document.getElementById('results1');    var fruits = [ 'Rum', 'Vodka', 'Whiskey', 'Beer' ];      for(var i = 0; i < fruits.length; i++){        var fruit = document.createElement('div').innerHTML = fruits[i];        results.appendChild(fruit);      }  })();  

Answer by Balachandran for Add to element as divs


Demo try like this

var parent = $("#results1");  var fruit = ['Rum', 'Vodka', 'Whiskey', 'Beer'];    $.each(fruit, function (i, val) {        parent.append("
" + val + "
"); });

Answer by Alex Char for Add to element as divs


You can use this:

js

(function() {      var results = document.getElementById('results1');      var fruit = [ 'Rum', 'Vodka', 'Whiskey', 'Beer' ];          for(var i=0; i" + fruit[i] + "
"); $(div).appendTo("#results1"); } })();

fiddle

Answer by Nick R for Add to element as divs


Just a simple for loop to iterate over the array, and then append each item to the results div.

 for (var i = 0 ; i < fruit.length; i++) {       results.innerHTML += "
" + fruit[i] + "
"; };

JSFiddle Demo

Answer by Nikhil Talreja for Add to element as divs


(function() {      var results = document.getElementById('results1');      var fruit = [ 'Rum', 'Vodka', 'Whiskey', 'Beer' ];      for(var i=0; i'+fruit[i]+'
'); } })();

Answer by Suraz for Add to element as divs


fruit.forEach(function(value,index){      var div = document.createElement('div');      div.innerHTML = value;      $("#results1").append(div);  });  

Answer by Arvind for Add to element as divs


You may try this one:

(function() {      var results = document.getElementById('results1');      var drink = [ 'Rum', 'Vodka', 'Whiskey', 'Beer' ];      results.innerHTML += '
' + drink.join('
') + '
'; })();

Answer by Ricardo Arteaga for Add to element as divs


This is the for loop you could use for javascript and incorporated it into your html.

 var drink = ["Rum", "Vodka", "Whiskey", "Beer"];   for (index = 0; index < drink.length; ++index) {       text += drink[index];   }   


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.