Method | Description |
---|---|
document.getElementById('question1') | Finding an element by the id attribute |
document.getElementsByClassName('multipleChoice') | Finding elements by the class attribute name |
document.forms['reservations'] | Finding elements by HTML form name. |
document.getElementsByTagName("p") | returns a Nodelist with all nodes whose markup tag is "p". You can
iterate through a node list using a for loop:
paraList=document.getElementsByTagName("p"); for (i=0; i < paraList.length; i++) { document.write(paraList[i].innerHTML); document.write("<br />"); } |
document.querySelector() | returns the first element found in the DOM that matches the CSS selector passed as the method's parameter. For example:
document.querySelector('#nameTable') |
Method | Description |
---|---|
document.write(text) | Writing into the HTML output stream.
|
document.getElementById(id).innerHTML= | changes the text contents of an element--use other methods to
modify the dom structure. For example:
document.getElementById('question1').innerHTML = "New Question!"; |
document.getElementById(id).attribute= | Changes the attribute's value. For example:
document.getElementById("image").src="landscape.jpg"; |
document.getElementById(id).style.attribute= | Changes the CSS style of an HTML element: allows you to enable/disable
form elements, change label's colors, etc. For example:
document.getElementById("p2").style.color="blue"; document.getElementById("beneficiary").style.disabled = false; |
Method | Description |
---|---|
document.createElement(tagname) | Create an HTML element with the given tagname. For example:
var newParagraph = document.createElement("p"); |
parent.insertBefore(newElement, target) | Inserts newElement before target. target becomes the next sibling for newElement. parent is the node that is target's parent. |
parent.appendChild(node) | Add an HTML element as the last child for the parent element |
parent.replaceChild(newChild, oldChild) | replace the HTML element represented by oldChild by the HTML element represented by newChild |
parent.removeChild(child) | Remove an HTML element from the parent element |
Method | Description |
---|---|
document.getElementById(id).onclick=function(){code} | Adding event handler code to an onclick event. For example, the
following fragment turns the header to red when the button is clicked:
<h1 id="id1">My Heading 1</h1> <button type="button" id="BigRed" onclick="document.getElementById('id1').style.color='red'"> Click Me!</button>Functions can be assigned to events using the HTML DOM as well: document.getElementById('BigRed').onclick=function() { document.getElementById('id1').style.color = 'red' } |
node.innerHTML = "New text"You can also change the value by accessing the text node directly and then modifying its nodeValue property:
node.firstChild.nodeValue = "New text"Technically you should use nodeValue because it is in the www standard and is guaranteed to always work. .innerHTML is an informal browser add-on that is supported by all the major browsers. Personally I use .innerHTML because it is more succinct.
The following mock html document will be the one we manipulate in the examples presented in this section:
This is a paragraph.
This is another paragraph.
<script> var para=document.createElement("p"); var para.innerHTML = "This is new."; var element=document.getElementById("div1"); element.appendChild(para); </script>You might also see older code that inserts a new node the following way:
var para=document.createElement("p"); var node=document.createTextNode("This is new."); para.appendChild(node); ...
var parent = document.getElementById("div1"); var sibling = document.getElementById("p1"); parent.insertBefore(para,sibling);
var parent = document.getElementById("div1"); var oldChild = document.getElementById("p1"); parent.replaceChild(para, oldChild);
var parent = document.getElementById("div1"); parent.innerHTML = "<p id='p1'><b>New Content!</b></p>";
For your project you will need to add a new row to the Assessments when the user presses the "New" button. Here is a simple example of how to add a row to a table:
<head> <style> table, td, th { border-collapse: collapse; border: 1px black solid; } th, td { padding: 5px } </style> <script> function handleClick() { let rowContent = "<tr><td>Brad</td><td>Vander Zanden</td><td>Knoxville, TN</td></tr>"; let tableRef = document.getElementById('nameTable').getElementsByTagName('tbody')[0]; let newRow = tableRef.insertRow(); newRow.innerHTML = rowContent; } </script> </head> <h2>Adding A Row to a Table </h2> <table id="nameTable"> <tr><th>First Name</th><th>Last Name</th><th>Address</th></tr> <tr><td>Wily</td><td>Coyote</td><td>Looney Tunes</td></tr> </table> <p> <button type="input" onclick="handleClick()">Add Row!</button>