Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7

Thread: Mistarobj

  1. #1
    New to the CF scene
    Join Date
    Jul 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Mistarobj

    I was wondering if anyone could let me know why my "add task" function isn't working for this to do list. It is an assignment that I must turn in and I finally got it right after a few hours but it was returned for re-sumbission because my add task function isn't working properly. I tried to take out the other functions and test it , but that failed. Then I tried to remove all functions and re-write them one by one and test them, but still the add function isn't working. I'm struggling here and I don't want an answer to homework just some help as to why/or what i'm doing wrong so I can fix it. Thank you in advance if anyone can help here is what I have.

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd.>
    <html>
    <head>
    <title>ToDoList</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    </head>
    <body>
    
    <h1>To Do List</h1>
    <form action="">
    <p>New Task <input type="text" size="68"
    name="newtask" />
    </p><p><input type="button" value="Add Task"
    onclick="addTask()"
    style="width: 150px" />
    <input type="button" value="Ascending Sort"
    onclick="ascendingSort()" style="width: 150px" />
    <input type="button" value="Descending Sort"
    onclick="descendingSort()" style="width: 150px" />
    </p>
    <p><select name="tasks" size="10"
    style="width: 500px">
    <option value="tasks">Tasks</option></select></p>
    </form>
    
    <script type="text/javascript">
    
    function addTask() {
    if (document.forms[0].newtask.value == "")
    window.alert("You must enter a value
    in the New Task field.");
    else {
    if (document.forms[0].tasks
    .options[0].value == "tasks")
    document.forms[0].tasks.options[0]
    = null;
    var newTask = new Option();
    newTask.value = document.forms[0]
    .newtask.value;
    newTask.text = document.forms[0]
    .newtask.value;
    var numTasks = document.forms[0].tasks
    .options.length;
    document.forms[0].tasks.options[numTasks]
    = newTask;
    document.forms[0].newtask.value = "";
    }
    }
    
    function deleteTask() {
    var selectedTask = 0;
    var taskSelected = false;
    while (selectedTask &lt; document.forms[0]
    .tasks.length) {
    if (document.forms[0].tasks
    .options[selectedTask].selected
    == true) {
    taskSelected = true;
    break;
    }
    ++selectedTask;
    }
    if (taskSelected == true)
    document.forms[0].tasks
    .options[selectedTask] = null;
    else
    window.alert("You must select a task in
    the list.");
    }
    
    function descendingSort() {
    var newTasks = new Array();
    for (var i =0; i &lt; document.forms[0].tasks
    .length; ++i) {
    newTasks[i] = document.forms[0].tasks
    .options[i].value;
    }
    newTasks.sort();
    newTasks.reverse();
    for (var j =0; j &lt; document.forms[0]
    .tasks.length; ++j) {
    document.forms[0].tasks.options[j].value
    = newTasks[j];
    document.forms[0].tasks.options[j].text
    = newTasks[j];
    }
    }
    
    
    
    /* <![CDATA[ */
    /* ]]> */
    </script>
    
    </body>
    </html>
    Last edited by VIPStephan; 07-28-2014 at 02:11 PM. Reason: fixed wrongly converted code

  • #2
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,980
    Thanks
    56
    Thanked 557 Times in 554 Posts
    Looking at your error console will tell you all you need to know. And learning how to use your error console is essential.

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    When posting here please help us to help you by making it easier to view, copy, modify, test and debug your scripts by following the posting guidelines and wrapping your code in CODE tags. This means use the octothorpe or # button on the toolbar.

    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.

  • #4
    The fat guy next door VIPStephan's Avatar
    Join Date
    Jan 2006
    Location
    Halle (Saale), Germany
    Posts
    8,695
    Thanks
    6
    Thanked 1,011 Times in 984 Posts
    Quote Originally Posted by Philip M View Post
    When posting here please help us to help you by making it easier to view, copy, modify, test and debug your scripts by following the posting guidelines and wrapping your code in CODE tags. This means use the octothorpe or # button on the toolbar.
    To be fair, there is no code button or hint about code BB tags in the quick reply window. This is a design flaw of the forum I’ve already complained about but you probably know about the motivation of the owners when it comes to working on pure usability enhancements.

  • #5
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,980
    Thanks
    56
    Thanked 557 Times in 554 Posts
    Quote Originally Posted by VIPStephan View Post
    To be fair, there is no code button or hint about code BB tags in the quick reply window. This is a design flaw of the forum I’ve already complained about but you probably know about the motivation of the owners when it comes to working on pure usability enhancements.
    agreed, but when starting a new thread you go straight to the "advanced view" so pages starting threads will always have the option of adding code tags. It would be simple (ish) enough to detect if someone is trying to paste code and plain text and prompt them to add code tags, but I'm guessing that one's a long way off...

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,869
    Thanks
    79
    Thanked 4,418 Times in 4,383 Posts
    Just for starters: You cannot break a string with a newline (unlike PHP and a few other languages).

    BAD CODE:
    Code:
    window.alert("You must enter a value
    in the New Task field.");
    working code:
    Code:
    window.alert("You must enter a value in the New Task field.");
    also working code:
    Code:
    window.alert("You must enter a value"
        + " in the New Task field.");
    **********
    Having said all that...alert( ) is considered very much obsolete and should be avoided in modern JS coding.
    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.

  • #7
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,869
    Thanks
    79
    Thanked 4,418 Times in 4,383 Posts
    Just for starters: You cannot break a string with a newline (unlike PHP and a few other languages).

    BAD CODE:
    Code:
    window.alert("You must enter a value
    in the New Task field.");
    working code:
    Code:
    window.alert("You must enter a value in the New Task field.");
    also working code:
    Code:
    window.alert("You must enter a value"
        + " in the New Task field.");
    **********
    Having said all that...alert( ) is considered very much obsolete and should be avoided in modern JS coding.
    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.


  •  

    Tags for this Thread

    Posting Permissions

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