Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
    <script src="https://code.jquery.com/jquery-3.1.0.js"></script>
<input id="task" type="text"/>
<select id="priority">
 <option id="Normal">Normal</option> 
<option id="Urgent">Urgent</option>
<option id="Critical">Critical</option>
<option id="If You Can">If You Can</option>
 </select>
<button onclick="amitFunction()">Add</button>
<hr/>
<table>
  <tr>
    <th id="result"></th>
<th id="priorit"></th>
  </tr>
<table>
</body>
</html>
 
var todo_list = [];
function amitFunction() {
    
  
/* Define vars and values  */
  
        var item = document.getElementById('task').value;
  /*  Check if text is less than 6 chars or more than 42 chars  */
        if(item.length<6){
             alert('Your text must have a least 6 chars');
             return;
        }else if(item.length>42){
            alert('Your text must have less than 42 chars');
            return;
        }
        var pro = document.getElementById('priority').value;
        var pro_array = ['Urgent','Critical','Normal'];
  
        var priorities = 
            {
              'Critical' : 0,
              'Urgent' : 1, 
              'Normal' : 2,
              'If You Can' : 3
            }
  
        todo_list.push(
          {
            priority : pro,
            task : item
          }
        );
  
        todo_list.sort(function (task1, task2) {
          return priorities[task1.priority] - priorities[task2.priority];
        });
  
        
        
        var resultNode = document.getElementById("result");
  
        while (resultNode.firstChild) {
          resultNode.removeChild(resultNode.firstChild);
        }
  
        var priorityNode = document.getElementById("priorit");
  
        while (priorityNode.firstChild) {
          priorityNode.removeChild(priorityNode.firstChild);
        }
  
        for(var i =0; i < todo_list.length; i++)
          {
            var lia = document.createElement("p");
            var lib = document.createElement("p");
  
            var item_list = document.createTextNode(todo_list[i].task);
            var item_pro = document.createTextNode(todo_list[i].priority);
 
            lia.appendChild(item_list);
            lib.appendChild(item_pro);
                        
            document.getElementById("result").appendChild(lia);
            document.getElementById("priorit").appendChild(lib);
            document.getElementById('task').value='';
            
              /*  Change text color base on priority  */
            if(todo_list[i].priority == pro_array[0]){
                 $("p:last-child").css('color','red');
               }
            if(todo_list[i].priority == pro_array[1]){
                 $("p:last-child").css('color','orange');
               }
             if(todo_list[i].priority == pro_array[2]){
                 $("p:last-child").css('color','green');
               }
  
         }
  
         var resultNode = document.getElementById("result");
         var priorityNode = document.getElementById("priorit");
  
        for(var i =0; i< resultNode.childNodes.length; i++) (function(i){ 
          resultNode.childNodes[i].onclick = function() {
            $([resultNode.childNodes[i],priorityNode.childNodes[i]]).css('color','gray');
            $([resultNode.childNodes[i],priorityNode.childNodes[i]]).css("text-decoration", "line-through");
          }
          priorityNode.childNodes[i].onclick = function() {
            $([resultNode.childNodes[i],priorityNode.childNodes[i]]).css('color','gray');
            $([resultNode.childNodes[i],priorityNode.childNodes[i]]).css("text-decoration", "line-through");
          }
        })(i);
        
  }
Output

This bin was created anonymously and its free preview time has expired (learn why). — Get a free unrestricted account

Dismiss x
public
Bin info
anonymouspro
0viewers