Skip to content Skip to sidebar Skip to footer

Dom Nodes Adding, Deleting, Alerts

So a little lost here, I'm not sure how to approach this, I've done the HTML and CSS but not sure how to do it in JS. My HTML: <

Solution 1:

This is the code with little explain

<!doctype html><htmllang="en"><head><title> Task 1 </title><metacharset="utf-8"><!--<script src="DomNodes.js"></script>--><script>functionmyFunction() {

    //get input uservar userInput=document.getElementById("input").value;

    //clean text areadocument.getElementById("input").value="";

    //create paragraphvar para = document.createElement("p");

    //assign value ->user inputvar node = document.createTextNode(userInput);

    //assign text at paragraph
    para.appendChild(node);
    
    //assign paragraph at div tagvar element = document.getElementById("output");
    element.appendChild(para);
  }

  functionremove_LastChild() {
  //get div outputvar select = document.getElementById('output');

  //control if there are child into output divif (select.childElementCount>0){
  //select last paragraph and remove it
  select.removeChild(select.lastChild);
  }
  else{
    alert("Tehere are not paragraph!");
  }

  }
  </script><style>#output {
      border: blue 5px solid;
      padding: 10px;
      margin-bottom: 10px;
      margin-top: 10px;
      width: 50%;
    }
  #outputp {
    padding:10px;
    border: black 1px dashed;
  }
  </style></head><body><h2> TASK 3 - Creating, Appending and Deleting Nodes in the DOM Tree </h2><p> Type in text below, click add to add as paragraph. <buttonid="add"onclick="myFunction()"> ADD </button></p><textareaid  ="input"rows="10"cols="60"></textarea><br><buttonid="delete"onclick="remove_LastChild()">Delete Last Paragraph</button><br><br><h2> Added Paragraphs </h2><divid="output"></div></body></html>

I hope this hel you

Post a Comment for "Dom Nodes Adding, Deleting, Alerts"