Not printing "id" when called in HTML from JavaScript - javascript

I am working on an assignment where I have to pull quote and a name from a randomized array of such and display them in my HTML page. under some filler text. For what ever reason the script is running and displaying a different id (a simple local date a time display) but not the quote randomizer. Any help would be greatly appreciated.
HTML
</head>
<body>
<h1>CIS - 16W</h1>
<nav id="nav">
<ul>
<li>Home</li>
<li>Assignment 2</li>
<li>Assignment 3</li>
<li>Assignment 4</li>
<li>Assignment 5</li>
<li>Assignment 6</li>
<li>Assignment 7</li>
<li>Final Project</li>
</ul>
</nav>
</body>
<meta charset="utf-8">
<meta name=“description” content=“description”>
<meta name=“author” content=“your”>
<link rel="stylesheet" href="../css/styles.css">
<script src="../functions.js"></script>
<main>
<p>My name is _____ and I am a junior here at the _____.<br>I'm very excited to get started and hone in my skills in the multimedia environment!
</p>
<p>I am taking this course to help not only fill the math requirment for a Bacholer of Science, but also to aid my Multimedia minor and Advertising major!</p>
<span id="quote"></span>
<span id="name"></span>
JavaScript
window.onload = function () {
setInterval(timeAndDate, 1000);
}
var quotes = ["\"How many cares one loses when one decides not to be something but to be someone.\"",
"\"If you want to test a man's character give him power.\"",
"\"I just hope that one day, perferably when we're both blind drunk, we can talk about it\"",
"\"Why do you look so sad?'<br>'Because you speak to me in words and I look at you with feelings.\"",
"\"Courage is what it takes to stand up and speak. Courage is also what it takes to sit down and listen.\""];
var names = ["-Coco Chanel", "-Abrham Lincoln", "-J. D. Salinger", "-Leo Tolstoy", "-Wiston Churchill"];
var arrIndex = Math.floor(Math.random() * (quotes.length));
function quoteGen() {
var quoteAll = document.getElementById("quote");
var namesAll = document.getElementById("name");
quoteAll.innerHTML = quotes[arrIndex];
namesAll.innerHTML = names[arrIndex];
if (arrIndex > quotes.length-1) {
arrIndex = 0;
}
}
function timeAndDate() {
var date = new Date();
var n = date.toDateString();
var time = date.toLocaleTimeString();
document.getElementById('localDate').innerHTML = n + ' ' + time;
}

There are 2 problems here,
You are not calling quoteGen which is updating the quotes, the timer need to call quoteGen but you are calling timeAndDate that is why the timing is getting updated, not the quotes
You need to generate the random index within quoteGen so that every invocation will get a new random value
window.onload = function() {
setInterval(quoteGen, 1000);
}
var quotes = ["\"How many cares one loses when one decides not to be something but to be someone.\"",
"\"If you want to test a man's character give him power.\"",
"\"I just hope that one day, perferably when we're both blind drunk, we can talk about it\"",
"\"Why do you look so sad?'<br>'Because you speak to me in words and I look at you with feelings.\"",
"\"Courage is what it takes to stand up and speak. Courage is also what it takes to sit down and listen.\""
];
var names = ["-Coco Chanel", "-Abrham Lincoln", "-J. D. Salinger", "-Leo Tolstoy", "-Wiston Churchill"];
var idx;
function quoteGen() {
//the looping is used to make sure that every call to quoteGen will update a new quote
do {
var arrIndex = Math.floor(Math.random() * (quotes.length));
} while (idx == arrIndex);
idx = arrIndex;
var quoteAll = document.getElementById("quote");
var namesAll = document.getElementById("name");
quoteAll.innerHTML = quotes[arrIndex];
namesAll.innerHTML = names[arrIndex];
}
<p>My name is _____ and I am a junior here at the _____.
<br>I'm very excited to get started and hone in my skills in the multimedia environment!
</p>
<p>I am taking this course to help not only fill the math requirment for a Bacholer of Science, but also to aid my Multimedia minor and Advertising major!</p>
<span id="quote"></span>
<span id="name"></span>

You are not calling quoteGen() anywhere.

Related

I have tried multiple ways, Can anyone tell me what's wrong with this array?

I am trying to make a random quote generator that generates a quote by the click of a button. I think I have done everything right, but once the button is clicked nothing happens.
Here is the code
HTML code
<div class="quote-box">
<p id = "quote-generator"> this is where the quote will go </p>
<button class="btn" onclick="function newQuote()"> Next </button>
</div>
JS code
var list = [
'/"Your mind will always believe what you tell it. Feed it faith. Feed it the truth. Feed it with love. /"',
'/"A problem is a chance for you to do your best./"',
'/"Learn how to be happy with what you have while you pursue all that you want./"',];`
const randomNumber = Math.floor(Math.random()*list.lenght);
function newQuote() {
document.getElementById("quotes-generator").innerHTML = list [randomNumber];`
}
Mistakes in code
Function call should be done on click with onclick="newQuote()" and not with onclick="function newQuote()"
Typo error in taking the length of list to generate random number. It should be const randomNumber = Math.floor(Math.random() * list.length); and not const randomNumber = Math.floor(Math.random() * list.lenght);
Id specified in HTML was quote-generator and in script was quotes-generator. They must be same.
Also you can move the random number generation logic to inside of the function to generate new random number each time the user clicks the button. Current random number generation happens only once and the value will not update when the user clicks the button
var list = [
'/"Your mind will always believe what you tell it. Feed it faith. Feed it the truth. Feed it with love. /"',
'/"A problem is a chance for you to do your best./"',
'/"Learn how to be happy with what you have while you pursue all that you want./"',
];
function newQuote() {
const randomNumber = Math.floor(Math.random() * list.length);
document.getElementById("quote-generator").innerHTML = list[randomNumber];
}
<div class="quote-box">
<p id="quote-generator"> this is where the quote will go </p> <button class="btn" onclick="newQuote()">
Next </button>
</div>
The minimal representation of your solution will be
const list = [
'"Your mind will always believe what you tell it. Feed it faith. Feed it the truth. Feed it with love. "',
'"A problem is a chance for you to do your best."',
'"Learn how to be happy with what you have while you pursue all that you want."',
];
newQuote = () => document.getElementById("quote-generator").innerHTML = list[Math.floor(Math.random() * list.length)];
<div class="quote-box">
<p id="quote-generator">
this is where the quote will go
</p>
<button class="btn" onclick="newQuote()"> Next</button>
</div>
In your example no need to escape character in array. If single quote ' inside in your array value you need escape character.
End of array you have extra , .
Wrong way to call function on button click.
var list = ['"Your mind will always believe what you tell it. Feed it faith. Feed it the truth. Feed it with love. "', '"A problem is a chance for you to do your best."', '"Learn how to be happy with what you have while you pursue all that you want."'];;
function newQuote() {
let rnd = Math.floor(Math.random() * list.length);
let rqt = list[rnd];
document.getElementById("quote-generator").innerHTML = rqt;
}
<div class="quote-box">
<p id="quote-generator"> this is where the quote will go </p> <button class="btn" onclick="newQuote()"> Next </button>
</div>
const randomNumber = Math.floor(Math.random()*list.lenght);
It should be list.length and not list.lenght.
If you want to avoid repetitions, it is probably a better idea to shuffle the array once and then present it to the user in a cycling fashion:
/* Randomize array in-place using Durstenfeld shuffle algorithm */
function shuffleArray(array) {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i],array[j]] = [array[j],array[i]];
}
}
const list = [
'"Your mind will always believe what you tell it. Feed it faith. Feed it the truth. Feed it with love. "',
'"A problem is a chance for you to do your best."',
'"Learn how to be happy with what you have while you pursue all that you want."',
];
shuffleArray(list);
list.n=-1;
document.querySelector(".btn").onclick=newQuote;
function newQuote(){
document.getElementById("quote-generator").innerHTML = list[++list.n%list.length];
}
newQuote();
<div class="quote-box">
<p id="quote-generator"> this is where the quote will go </p>
<button class="btn"> Next </button>
</div>

Javascript: My .textContent works, but not my innerHTML

I have a strange problem. In general, when I set a variable as textContent, it shows it, but it doesn't when I set it as innerHTML
More precisely
Here is the HTML
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Dynamic Menu</title>
</head>
<body id="top">
<h1>The Extremadura Region of Western Spain</h1>
<h2 >Geography Of The Region</h2>
<p>The autonomous community of Extremadura is in western Spain alongside the Portuguese border.
It borders the Spanish regions of Castilla y Leon, Castilla La Mancha and Andalucía as well as Portugal (to the West).
Covering over 40,000 square kilometers it has two provinces: Cáceres in the North and Badajoz in the South.</p>
<h2>Where To Stay</h2>
<p>There is a wide range of accommodation throughout Extremadura including small inns and guest houses ('Hostals') or
think about renting a 'casa rural' (country house) if you are travelling in a group.</p>
<h2>Climate</h2>
<p>Generally Mediterranean, except for the north, where it is continental. Generally known for its extremes,
including very hot and dry summers with frequent droughts, and its long and mild winters.</p>
<h2>What To See</h2>
<p>Extremadura hosts major events all year round including theater, music, cinema, literature and folklore.
Spectacular venues include castles, medieval town squares and historic centers.
There are special summer theater festivals in the Mérida, Cáceres, Alcántara and Alburquerque.</p>
<h2>Gastronomy</h2>
<p>The quality of Extremaduran food arises from the fine quality of the local ingredients.
In addition to free-range lamb and beef, fabulous cheeses, red and white wines, olive oil, honey and paprika,
Extremadura is particularly renowned for Iberian ham. The 'pata negra' (blackfoot) pigs are fed on acorns in the
cork-oak forests, the key to producing the world's best ham and cured sausages.</p>
<script src="lunch.js"></script>
</body>
</html>
Here is the script
function lop(){
var hs = document.getElementsByTagName('h2');
for(var g = 0; g<hs.length; g++){
hs[g].setAttribute('id', g);
}
var budy = document.getElementById('top'); //Gets the body id
var nnn = document.createElement('nav'); //Creats a nav event
var uuu = "<ul > \
<li id='one'> <a href='#0'>Geography Of The Region </a> </li> \
<li id='two'> <a href='#1'>Where To Stay </a> </li> \
<li id='tre'> <a href='#2'>Climate </a> </li> \
<li id='for'> <a href='#3'>What To See</a> </li> \
<li id='fiv'> <a href='#4'>Gastronomy</a> </li>";
// li: 55-60 make the HTML
nnn.innerHTML = uuu; //Sets the HTML to the nav
var h = document.getElementsByTagName('h2')[0]; // Get the specific element
budy.insertBefore(nnn, h); // inserts the element nav and the whole html before h
var ps = document.getElementsByTagName('p');
var hih = ' AAAAA ';
for(var g = 0; g<ps.length; g++){
ps[g].nextSibling.innerText = hih;
}
}
lop(); //cals the function so it executes
So basicly in this exercise i have to create an ul within the script and without modyfing the HTML.
I successed in creating an ul. Then I have to creat a link that brings me to the top of the page. Which is this part here:
var ps = document.getElementsByTagName('p');
var hih = ' AAAAA ';
for(var g = 0; g<ps.length; g++){
ps[g].nextSibling.innerText = hih;
}
Here I try to creat a link that bring me back to the top. Im using the advantage that chrome has blank space betwwen sibling to creat that link in there.
The problem is that it doesn't show. When I go to my debugger, I have no errors, but nothings shows. If change ps[g].nextSibling.innerText = hih; for .textContent it shows the whole think.
I know the difference between .innerHTML and .textContent (or I think), so why doesn't it show my link and can I make it show ?
I don't understand you use nextSibling. If you want to use innerHTML, you can use bellow script
ps[g].innerHTML = ps[g].innerHTML + hih;
You can read about nextSibling in https://www.w3schools.com/jsref/prop_node_nextsibling.asp
Ok so a friend of mine changed my procedure and here what he did:
var ps = document.getElementsByTagName('p');
var hih = '<br /><br /> To the top ';
for(var g = 0; g<ps.length; g++) {
ps[g].innerHTML += hih;
}
In other words, what I learned from this 3h of js, is that you can't change a blank child's HTML, but only his text.

Create html element with jquery based on user input

So after searching a while I haven't been able to find an existing question that seems to address this in a way that I can relate to with my specific issue. If there is already a good thread on this that I missed, my apologies in advance and feel free to just post a link to it and call me a dummy!
In plain english, here's the goal: I basically want to generate some html with jquery but with a couple of twists. There will basically be two sets of content that will alternate with every other number, I'll call them content-a and content-b. The user is prompted to enter a number, let's say user enters 4. Upon submitting this value, the markup is then generated like so: (1)content-a (2)content-b (3)content-a (4)content-b.
So here's a bit of code that hopefully will help a little.
I'm aware of how to generate html, but that's about as far as I've gotten so far, my js is definitely a weak point and needs lots of practice:
$("#targetDIV").html("<h1>Hello World!</h1> <p>This is a big fat test</p>");
The markup is simple enough, almost seems pointless to post it in here since it's kind of obvious but I'll do it anyway:
<div id="targetDIV" style="border: 3px solid purple">
</div>
The desired output though would be something like this, based on the value the user chooses but let's just stick with the 4 example:
<div id="targetDIV" style="border: 3px solid purple">
<!-- Content A -->
<h1>Hello World!</h1>
<p>This is a big fat test</p>
<!-- Content B -->
<h1>Hello Universe!</h1>
<p>This is a super mega big fat test</p>
<!-- Content A -->
<h1>Hello World!</h1>
<p>This is a big fat test</p>
<!-- Content B -->
<h1>Hello Universe!</h1>
<p>This is a super mega big fat test</p>
</div>
Hopefully there's enough here to go on or to at least point me in the right direction, thanks in advance for any wisdom any of you might offer up!
Here is a full, working live example that does exactly what you're looking for.
The following code will take a numerical input from the user, then append alternating sets of content according to the number the user inputted:
var num = prompt("Enter a number");
var contenta = "<h1>Hello World!</h1> <p>This is a big fat test</p>";
var contentb = "<h1>Hello Universe!</h1> <p>This is a super mega big fat test</p>";
var targetDiv = $("#targetDIV");
console.log(targetDiv);
for (var i = 0; i < num; i++) {
if (i % 2 === 0) targetDiv.append(contenta);
else targetDiv.append(contentb);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="targetDIV" style="border: 3px solid purple">
</div>
You could assign the two html strings to the indices of an array.
var str1 = "<h1>Hello World!</h1> <p>This is a big fat test</p>"
var str2 = "<h1>Hello Universe!</h1> <p>This is a super mega big fat test</p>"
var responses = [str1, str2];
Then, you can use a for loop that will repeat as many times as the user's input.
And in each iteration of the loop, you could perhaps $('#targetDIV').append(responses[i % 2]);
You could do something like this.
$('#number').change(function() {
var ind = $(this).val()
$('#target-div').append($('#holder div').get(ind))
});
This keeps the HTML in a hidden div, then extracts your desired content by its index. Not the best way but works.
JSFiddle
If you are simply alternating between two content sets, you can simply store them as a JS array, say content, and generate/insert them on the fly.
The key is to empty your target element when the user updates the change count, and access the correct element in the array based on the modulus of the array size, i.e. content[i%content.length]. This method allows you to arbitarily increase the size of your content array, and the script will keep inserting elements by going through the list, and repeat from the start when it reaches the end.
$(function() {
var content = [
'<h1>Hello World!</h1> <p>This is a big fat test</p>',
'<h1>Hello Universe!</h1><p>This is a super mega big fat test</p>'
];
$('#count').on('change', function() {
$('#targetDIV').empty();
var count = parseInt($(this).val());
for (var i = 0; i < count; i++) {
$('#targetDIV').append(content[i%content.length]);
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="count" type="number" min="0" step="1" value="0" placeholder="0" />
<div id="targetDIV" style="border: 3px solid purple"></div>
I am not an expert js dev but cooked something quick and easy for you.
link to codepen:http://codepen.io/anon/pen/OVdMow
$(function() {
var A = '<!-- Content A --><h1>Hello World!</h1><p>This is a big fat test</p>';
var B = '<!-- Content B --><h1>Hello Universe!</h1><p>This is a super mega big fat test</p>';
var targetDiv = $('#targetDIV');
$('#listCount').on('input', function() {
targetDiv.empty();
for(var i = 0; i < +this.value; i++) {
targetDiv.append( i % 2 == 0 ? A : B );
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="number" name="listCount" id="listCount"/>
<div id="targetDIV" style="border: 3px solid purple">
</div>
function generateElments() {
var num = $("#numOfElements").val();
var writeA = true;
var aElement = "<p>I am <strong>A</strong>!</p>";
var bElement = "<p>I am <strong>B</strong>!</p>";
for (var i = 0; i < num; i++) {
$("#elements-container").append(writeA ? aElement : bElement);
writeA = !writeA;
};
};
Here is a working plunker of what you need!
http://plnkr.co/edit/qI1LtBwDwu7KIKFzUehB?p=preview

how to concatenate text with image in a variable

Hello first i am basic to java script i have a some on mouse over divs which every one
contains information along images. the problem is that i want to combine text with an image
inside a variable whenever i am mouse hovering each divs so the information along the image
should change as i did program for every one of them
the problem is that how to combine the text and image inside a variable
but i don't know how to do that here is the code:
<script type="text/javascript">
function ENGshowElements(){
var Engineering = "Your In Engineering Section <br> <img src='a.jpg'>";
document.getElementById("contents").innerHTML = Engineering;
}
function CONshowElements(){
var Construction = "Your In Construction Section";
document.getElementById("contents").innerHTML = Construction;
}
function LLCshowElements(){
var LLCDubia = "Your In LLC Dubia Section";
document.getElementById("contents").innerHTML = LLCDubia;
}
function WASshowElements(){
var WasteManagement = "Your In Waste Management Section";
document.getElementById("contents").innerHTML = WasteManagement;
}
function TRAshowElements(){
var Transportation = "Your In Transportation Section";
document.getElementById("contents").innerHTML = Transportation;
}
function LOGshowElements(){
var Logistics = "Your In Logistics Section";
document.getElementById("contents").innerHTML = Logistics;
}
</script>
<div class="firstbox" id="Engineering" onmouseover="ENGshowElements(); return false; " >Engineering</div>
<div class="secbox" id="Construction" onmouseover="CONshowElements(); return false; ">Construction</div>
<div class="thirdbox" id="LLCDubia" onmouseover="LLCshowElements(); return false; " >LLC Dubia</div>
<div class="fourthbox" id="WasteManagement" onmouseover="WASshowElements(); return false; " >Waste Management</div>
<div class="fivthbox" id="Transportation" onmouseover="TRAshowElements(); return false; " >Transportations</div>
<div class="sixthbox" id="Logistics" onmouseover="LOGshowElements(); return false; " >Logistics</div>
DEMO: jsFiddle
HTML:
<div class="firstbox" id="Engineering">Engineering</div>
<div class="secbox" id="Construction">Construction</div>
<div class="thirdbox" id="LLCDubia">LLC Dubia</div>
<div class="fourthbox" id="WasteManagement">Waste Management</div>
<div class="fivthbox" id="Transportation">Transportations</div>
<div class="sixthbox" id="Logistics">Logistics</div>
<div id="contents"></div>
JS:
document.getElementById('Engineering').onmouseover = ENGshowElements;
document.getElementById('Construction').onmouseover = CONshowElements;
document.getElementById('LLCDubia').onmouseover = LLCshowElements;
document.getElementById('WasteManagement').onmouseover = WASshowElements;
document.getElementById('Transportation').onmouseover = TRAshowElements;
document.getElementById('Logistics').onmouseover = LOGshowElements;
function ENGshowElements() {
var Engineering = "Your In Engineering Section <br> <img src='a.jpg'>";
document.getElementById("contents").innerHTML = Engineering;
}
function CONshowElements() {
var Construction = "Your In Construction Section";
document.getElementById("contents").innerHTML = Construction;
}
function LLCshowElements() {
var LLCDubia = "Your In LLC Dubia Section";
document.getElementById("contents").innerHTML = LLCDubia;
}
function WASshowElements() {
var WasteManagement = "Your In Waste Management Section";
document.getElementById("contents").innerHTML = WasteManagement;
}
function TRAshowElements() {
var Transportation = "Your In Transportation Section";
document.getElementById("contents").innerHTML = Transportation;
}
function LOGshowElements() {
var Logistics = "Your In Logistics Section";
document.getElementById("contents").innerHTML = Logistics;
}
=> jsfiddle http://jsfiddle.net/Gy5rH/
just for the sake of making things clear, and pointing out that you're probably going the wrong way, here is a better alternative. It could probably be done mostly using css but here's something more easier to maintain.
Instead of using multiple triggers, We will use only one click function on every button. Each button will have a "data-target" which is the id of an other element.
Our markup will look like this:
<html>
<head>
<style>
#source { display: none; }
</style>
<script>
// Our click event
function clickEvent (ev) {
// Get the target in the dom
// While checking more about event should be good because
// Target may not be the element you're looking for in some cases.
var target = ev.target.dataset['target'];
var obj = document.getElementById(target);
// change the content with the one found
document.getElementById('content').innerHTML = obj.innerHTML;
}
function loaded() {
var docs = document.getElementsByClassName("btn");
// Transform to array
docs = Array.prototype.slice.call(docs);
docs.forEach(function(elem) {
// Assign click event to all elements
elem.onclick = clickEvent;
});
}
</script>
</head>
<body onload="loaded()">
<div>
<button class="btn" data-target="Engineering-Source">Engineering</button>
...
</div>
<div id="content"></div>
<div id="source">
<div id="Engineering-Source">
Your In Engineering Section <br> <img src='a.jpg'>
</div>
...more after
</div>
</body>
</html>
I added comments, but it's a nice way to do. Avoiding "vanillajs" might not be a bad thing but. It can be done without much pain in vanillajs.
That said, here are some reasons why it's good. The content remains in the html instead of javascript. If a web spider will download your page, there are far more chances that it will look for html content instead of text in javascript source.
In my example, some things might not exists in old browser like "dataset" and "forEach".
Note
That said, a pure "css" way of doing this is possible but might make the structure of the document harder to edit. On the other hand, there are ways to mix css and js to keep a minimum of js and as much as possible html/css to keep the styles in line.
Anyway, my example above should be working in some browsers to give an idea how to do it. I recommend using libraries like jQuery or prototype if you're not familiar with Javascript yet. The code above shouldn't definitely end up in production.

Output variable into html

How do I output javascript into the html below. I've been trying to get anything to display, but the only thing that displays is "object,object"
<h2>title</h2>
<ul>
<li>rating</li>
<li>year</li>
<li>length</li>
<li>comedy</li>
<li>main characters</li>
</ul>
Thank you for you help everyone. I really appreciate it.
Pure JavaScript can be a little nasty on the eyes sometimes:
for (var i = 0; i < movieList.length; i++) {
document.body.appendChild(document.createElement('h2')).appendChild(document.createTextNode('title: ' + movieList[i].title));
var cUL = document.body.appendChild(document.createElement('ul'));
cUL.appendChild(document.createElement('li')).appendChild(document.createTextNode(movieList[i].rating));
cUL.appendChild(document.createElement('li')).appendChild(document.createTextNode(movieList[i].year));
cUL.appendChild(document.createElement('li')).appendChild(document.createTextNode(movieList[i].length));
cUL.appendChild(document.createElement('li')).appendChild(document.createTextNode(movieList[i].isComedy));
cUL.appendChild(document.createElement('li')).appendChild(document.createTextNode('main characters: ' + movieList[i].mainCharacters.join(", ")));
}
Demo here.
Here is your HTML:
<body id="thebody">
<h2>title: Goodfellas</h2>
<ul>
<li>rating: R</li>
<li>year: 1990</li>
<li>length: 3.25</li>
<li>comedy</li>
<li>main characters: James Conway, Henry Hill</li>
</ul>
</body>
HEre is your JS
var list = document.createElement("ul");
for (var key in movieList) {
var title = document.createElement("li");
var titleText = document.createTextNode("title: " + movieList[key].title);
title.appendChild(titleText);
list.appendChild(title);
}
var _body = document.getElementById('thebody');
_body.appendChild(list);
Here is the demo of course do this with every property
make your list and items into a template and cloning using jquery so you can insert the data into the elements. It's a relatively simple pattern to produce.
var divContainer = $('#divContainer');
for ( var i = 0; i < array.length; i += 1 )
divContainer.append
(
$('<ul></ul>').append
(
$('<li><li>').innerHtml(" prop Name " + array[i].propName)
)
);
Since it seems you are just getting started, here is a good little reference to get you started in the right direction. I wouldn't rely on a book chapter by chapter to get where you want to go. It's tedious and unrealistic. Make a goal and do some research, take it a reasonable and applicable problem at a time instead of tackling the whole of the language right off.
Here's a quick solution if you happen to be using jQuery:
Example (jsFiddle)
// loop through the movie list.
$.each(movieList, function() {
$('<h2>'+this.title+'<h2>').appendTo('#movies');
$('<ul>').appendTo('#movies');
$('<li>'+this.rating+'</li><li>'+this.year+'</li><li>'+this.length+'</li><li>'+this.isComedy+'</li>').appendTo('#movies');
// open the main characters list item.
var charLi = '<li>main characters: ';
$.each(this.mainCharacters, function() {
charLi += this + ', ';
});
// remove the extra comma and space.
charLi = charLi.substring(0, charLi.length - 2);
// close the list item.
charLi += '</li>';
$(charLi).appendTo('#movies');
$('</ul>').appendTo('#movies');
});

Categories