JS HTML DOM
Now, this is the most important part of javascript, its use in DOM and uses in web development.
- *DOM ** stands for document object model, and is a structure that html takes on. It is a node and branch structure, the diagram is posted below:
Consider this html code:
This tree represents the code snippet above:
In other words, the HTML DOM model is a tree of objects. With this model, javascript has the power to create dynamic html.
HTML DOM Methods
- The HTML DOM Methods are actions you can perform on HTML elements
- The HTML DOM properties are values (of HTML elements) that you can set or change
- In the DOM, all HTML elements are defined as objects.
- A property is a value that you can get or set (like changing the content of an HTML element).
- A method is an action you can do (like add or deleting an HTML element).
For example, consider the following code snippet:
Above, getElementById() is the
is a method and innerHTML
is a property.
Finding HTML Elements
Changing HTML Elements
Adding and deleting elements
Adding event handlers
Finding HTML Objects
There are many different ways we can find select HTML objects. Read over them, below is a link of the list on W3Schools:
https://www.w3schools.com/js/js_htmldom_document.asp
DOM Elements
Now, lets look at how to find and access HTML elements. In JS, I will often want to manipulate HTML elements. So, to do so, I need to be able to find the elements I want to work with. There are several ways to do this:
- Finding HTML elements by id
- Finding HTML elements by tag name
- Finding HTML elements by class name
- Finding HTML elements by CSS selectors
- Finding HTML elements by HTML object collections
Consider the code below:
Here is an example of finding an HTML element by ID. If an element is not found, element will contain null
.
DOM HTML
β Changing HTML Content
The HTML DOM allows Javascript to change the content of HTML elements. The easiest way to modify the content of an HTML element is by using the innerHTML property. To change the content of an HTML element, use this syntax.
β Changing the value of an attribute
To do this, we use the syntax:
For example, consider the code below:
In the above code, the original photo was smiley.gif
, but the script changed it to landscape.jpg
. So, now the document shows the second picture instead of the first one.
NOTE: The above code as .src
since we are changing the src
attribute. If weβre changing a different attribute, we would need to make sure to use that attribute.
β document.write()
This document.wrirte()
can be used to write directly to the HTML output stream.
Look at the code below:
The above code, when it reaches document.write(Date());
calls the buil-in function Date()
and prints the the return of that function the the document. Again, donβt use this after a document has loaded, for example as the response to an action, as it will wipe the rest of the html.
DOM Forms
HTML form validation can be done by Javascript. If a form filed (fname) is empty, this function alerts a message, and returns false. Consider the following:
The above form is used below:
Javascript can validate numeric input
The below code is a way to ensure a form input is valid integer from 1 to 10.
We can also make this type of validation automatic. HTML form validation can be performed automatically by the browser. If a form field (fname) is empty, the required
attribute prevenets the form from being submitted.
An example code snippet of such an action is as follows:
keep in mind that the name
attribute is quite literally a βnameβ for the element. The name
attribute can be used as a reference when the data is submitted. The type
attribute is used for different for different HTML elements. For example:
- For
<button>
elements, the type specifies the type of button being used- e.g.
reset
,submuit
, etc.
- e.g.
- For
<input>
elements, the types specifies type of input
Reference documentation when such information is needed.
Above, we have the action attribute and it specifies the form-handler that will process the submitted form data. Form data is mostly submitted to a server-side handler, but it can also be JavaScript on the client. Above is an example of form data being sent to server-side hanlder. We can also do this on the client via js:
Data Validation
Data validation is the process of ensuring that user input is clean, correct, and useful. Most often, the purpose of data validation is to ensure correct user input.
- Server side validation is performed by a web server, after input has been sent to the server.
- Client side validation is performed by a web browser, before input is sent to a web server.