www.webdeveloper.com
+ Reply to Thread
Results 1 to 4 of 4
  1. #1
    Join Date
    Jul 2012
    Posts
    28

    please help displaying item - beginner student

    Hello I'm taking an intro javascript class an one of the last assignments is to write a web application to add items to a list using a form text input and button, and allow the user to delete an item from the list by clicking on it.

    Now I got all of the code working, except for when the item is displayed on the HTML is comes up as undefined.

    Please help

    <!doctype html>
    <html lang="en">
    <head>
    <title> Add and delete items Objective </title>
    <meta charset="utf-8">
    <style>
    p {
    font-style: italic;
    }
    li:hover {
    cursor: pointer;
    }

    </style>
    <script>
    /* create constructor to display the properties, in this case there's only one */
    function Item(name) {
    this.title = name;

    /*this will print it intp the page */
    this.print = function() {
    var s = "Item: " + this.name;
    return s;
    }
    }

    /* empty array where the items will be saved and displayed into the page */
    var itemList = [];

    window.onload = init;

    function init() {
    var submitButton = document.getElementById("submitButton");
    submitButton.onclick = getItemData;

    // add click handler for element created in javascript
    var itemList = document.getElementById("itemList");
    itemList.onclick = deleteItem;

    }

    /* form collection for the item
    when i enter an item it shows up as undefined in the html */

    function getItemData() {
    var itemInput = document.getElementById("item");
    var item = itemInput.value;


    if (item == null || item == "") {
    alert("Please enter an item.");
    return;
    }

    /* if something is entered then it is pushed and saved into the itemList */
    else {
    var items = new Item(name);
    itemList.push(items);
    addItemToList(items);
    }
    }

    /* this creates the new elements and displays them into the page once they are
    saved into the itemList */
    function addItemToList(items) {
    var itemList = document.getElementById("itemList");
    var li = document.createElement("li");
    li.innerHTML = items.print();
    itemList.appendChild(li);
    }

    /* this deletes the items from the page */
    function deleteItem() {
    var li = document.getElementById("itemList");
    if (li.firstChild) {
    li.removeChild(li.firstChild);
    }
    }
    </script>
    </head>
    <body>
    <form>
    <label for="item">Add an item: </label>
    <input id="item" type="text" size="20"><br>
    <input id="submitButton" type="button" value="Add!">
    </form>
    <ul id="itemList">
    </ul>
    <p>
    Click an item to remove it from the list.
    </p>
    </body>
    </html>

  2. #2
    Join Date
    Mar 2007
    Location
    U.K.
    Posts
    1,062
    Code:
    function Item(name) {
          this.title = name;
          
       /*this will print it intp the page */           
          this.print = function() {
            var s = "Item: " + this.name; /* Doesn't exist */
            return s;
          }
        }
    
    .......................
    
    function getItemData() {
          var itemInput = document.getElementById("item");
          var item = itemInput.value;
            
      
          if (item == null || item == "") {
            alert("Please enter an item.");
            return;
          }
          
    /* if something is entered then it is pushed and saved into the itemList */    
          else {
            var items = new Item( name ); /* Doesn't exist */
            itemList.push(items);        
            addItemToList(items);           
          }                
        }
    Where used, return should be executed unconditionally and always as the last statement in the function.

    That's my signature, it's not part of the damn post!

  3. #3
    Join Date
    Jul 2012
    Posts
    28
    what do you mean by it Doesn't exist */ ??

    Quote Originally Posted by Logic Ali View Post
    Code:
    function Item(name) {
          this.title = name;
          
       /*this will print it intp the page */           
          this.print = function() {
            var s = "Item: " + this.name; /* Doesn't exist */
            return s;
          }
        }
    
    .......................
    
    function getItemData() {
          var itemInput = document.getElementById("item");
          var item = itemInput.value;
            
      
          if (item == null || item == "") {
            alert("Please enter an item.");
            return;
          }
          
    /* if something is entered then it is pushed and saved into the itemList */    
          else {
            var items = new Item( name ); /* Doesn't exist */
            itemList.push(items);        
            addItemToList(items);           
          }                
        }

  4. #4
    Join Date
    Jul 2012
    Posts
    28
    Edit** I got it, thanks!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center



Recent Articles