...

View Full Version : Need help making a Javascript array assignment.



ThirstForBLOOD
11-03-2012, 01:36 AM
Our class needs to make a JavaScript array for an ordered list, i.e. 1: hot dogs 2: chicken 3: burgers, etc. There needs to be an add and delete button that will add on to the list and delete from the list. If the user enters something to delete that's not in the list they will be prompted that the item is not in the list.

There also needs to be a box that says "Char count" that displays the amount of characters in the string of list currently being shown to the user.



This is my current code for the assignment:

http://pastebin.com/iFadYXax

And also, this is the powerpoint that describes the assignment:

http://www.2shared.com/document/fPLwcCpa/Hw6.html

Old Pedant
11-03-2012, 06:58 AM
This is *NOT* an "ordered list":
<span>chicken</span><span>HotDogs</span><span>Steak</span><span>Hamburgers</span></span>

An ordered list uses the <ol> tag and then <li> element tags.

Philip M
11-03-2012, 09:30 AM
As a general rule, the people helping out in this forum don't write code for others (especially code that appears to be for homework), but try to help with fixing code that doesn't work. You may perhaps get someone to write this script for you, but you'll be far more likely to get help if you have made a substantial effort and written some code yourself. Then someone here will almost certainly help you correct/improve your work.

ThirstForBLOOD
11-04-2012, 12:13 AM
As a general rule, the people helping out in this forum don't write code for others (especially code that appears to be for homework), but try to help with fixing code that doesn't work. You may perhaps get someone to write this script for you, but you'll be far more likely to get help if you have made a substantial effort and written some code yourself. Then someone here will almost certainly help you correct/improve your work.

I have made an effort and I have written code that I posted. Are you not able to read?

Old Pedant
11-04-2012, 12:15 AM
Actually, Philip, he's done a fairly decent amount of work, already. Including some "unobtrusive" JavaScript.

Example (from his link):


document.getElementById("addOption").onclick = function() {
var newOption = prompt("What would you like to add? (Click cancel to abort)");
options.push(newOption);
var newOptionElement = document.createElement("span");
var newOptionElementText = document.createTextNode(newOption);
newOptionElement.appendChild(newOptionElementText);
document.getElementById("placeholder").appendChild(newOptionElement);
};

Kind of impressive, to tell the truth. But wrongheaded in that it doesn't qualify as an "ordered list".

felgall
11-04-2012, 01:44 AM
Actually, Philip, he's done a fairly decent amount of work, already. Including some "unobtrusive" JavaScript.

A lot of people will not have seen any of the code because he posted a link to his page (which a lot of people wouldn't risk clicking on because it could potentially be a link to a virus instead) instead of posting the code directly.

Old Pedant
11-04-2012, 05:39 AM
Ehhh...I'm either foolish or I trust my anti-virus program. Since I installed Microsoft Security Essentials (a free download) about 2 years ago, I have never gotten a virus and I've only had on minor malware. A better track record than I ever had with Norton, CA, or Kapersky, which I had used in the 12 years previously.

Philip M
11-04-2012, 10:34 AM
A lot of people will not have seen any of the code because he posted a link to his page (which a lot of people wouldn't risk clicking on because it could potentially be a link to a virus instead) instead of posting the code directly.

Yes, that is why I did not view his code. Just because I am paranoid that does not prove that it is not malware.

ThirstForBLOOD
11-04-2012, 10:30 PM
For people afraid of external links, here is my code. Still having trouble adding numbers and colons before each item in my list. I'm also not sure how to have the character box inform the user how many characters there currently are in the string.




<html><head>

<title>Document Object</title>

<style type="text/css">
div.topbox {
width:1220px;
padding:10px;
border:5px solid gray;
margin:0px;
background-color: #e1e1e1;
font-size: 20px
}
div.bottombox{
width:200px;
padding:10px;
border:5px solid gray;
margin:0px;
background-color: #0000ee;
font-size: 20px;
}
.placeholder
{
background-color: #ccff33;
font-size: 22px
}
</style>

</head>

<body>

<div class="topbox">

Here is my (Tom Rabovsky)list:<span class="placeholder" id="placeholder">

<span>chicken</span><span>HotDogs</span><span>Steak</span><span>Hamburgers</span></span>

</div>

<br />

Click button to start

<input type="button" id="addOption" value="Add a new element">
<input type="button" id="deleteOption" value="Delete an element">

<br />

<div class="bottombox">

Total Chars:

</div>

<script>
window.onload = function() {

var arrayOfTags = document.getElementById("placeholder").getElementsByTagName("span");

options = [];
for (var i = 0; i <= arrayOfTags.length - 1; i++) {
options.push(arrayOfTags[i].childNodes[0].nodeValue);
};

document.getElementById("addOption").onclick = function() {
var newOption = prompt("What would you like to add? (Click cancel to abort)");
options.push(newOption);
var newOptionElement = document.createElement("span");
var newOptionElementText = document.createTextNode(newOption);
newOptionElement.appendChild(newOptionElementText);
document.getElementById("placeholder").appendChild(newOptionElement);
};
document.getElementById("deleteOption").onclick = function() {
var deleteOption = prompt("What would you like to delete? (Click cancel to abort)");
var exists = false;
for (var i = 0; i <= options.length - 1; i++) {
if (deleteOption === options[i]) {
document.getElementById("placeholder").removeChild(document.getElementById("placeholder").getElementsByTagName("span")[i]);
options.splice(i, 1);
i = options.length - 1;
exists = true;
};
};
if (exists === false) {
alert("This item does not exist. Please re-enter.");
};
};
};
</script>

</body></html>

Old Pedant
11-05-2012, 12:32 AM
Aside from the fact that you are NOT using an ordered list, and so as a result the page looks as ugly a pig snot, what is your problem/question?

It seemed to me to work as you wanted it to.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum