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.
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.
__________________
All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
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?
Actually, Philip, he's done a fairly decent amount of work, already. Including some "unobtrusive" JavaScript.
Example (from his link):
Code:
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".
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
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.
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.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
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.
__________________
All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
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.
Code:
<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>