Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-03-2012, 12:36 AM   PM User | #1
ThirstForBLOOD
New to the CF scene

 
Join Date: Nov 2012
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
ThirstForBLOOD is an unknown quantity at this point
Need help making a Javascript array assignment.

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
ThirstForBLOOD is offline   Reply With Quote
Old 11-03-2012, 05:58 AM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,542
Thanks: 62
Thanked 4,054 Times in 4,023 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
__________________
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.
Old Pedant is offline   Reply With Quote
Old 11-03-2012, 08:30 AM   PM User | #3
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,098
Thanks: 197
Thanked 2,421 Times in 2,399 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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.
Philip M is online now   Reply With Quote
Old 11-03-2012, 11:13 PM   PM User | #4
ThirstForBLOOD
New to the CF scene

 
Join Date: Nov 2012
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
ThirstForBLOOD is an unknown quantity at this point
Quote:
Originally Posted by Philip M View Post
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?
ThirstForBLOOD is offline   Reply With Quote
Old 11-03-2012, 11:15 PM   PM User | #5
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,542
Thanks: 62
Thanked 4,054 Times in 4,023 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Old 11-04-2012, 12:44 AM   PM User | #6
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,528
Thanks: 0
Thanked 503 Times in 494 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
Quote:
Originally Posted by Old Pedant View Post
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.
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
felgall is offline   Reply With Quote
Old 11-04-2012, 04:39 AM   PM User | #7
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,542
Thanks: 62
Thanked 4,054 Times in 4,023 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Old 11-04-2012, 09:34 AM   PM User | #8
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,098
Thanks: 197
Thanked 2,421 Times in 2,399 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by felgall View Post
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.
Philip M is online now   Reply With Quote
Old 11-04-2012, 09:30 PM   PM User | #9
ThirstForBLOOD
New to the CF scene

 
Join Date: Nov 2012
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
ThirstForBLOOD is an unknown quantity at this point
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>
ThirstForBLOOD is offline   Reply With Quote
Old 11-04-2012, 11:32 PM   PM User | #10
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,542
Thanks: 62
Thanked 4,054 Times in 4,023 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
__________________
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.
Old Pedant is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:18 AM.


Advertisement
Log in to turn off these ads.