Working with DOM elements
Everything is node in HTML Document. Node can be of below types.
- document node
- element node
- text node
- attribute node
- comment node
Node object has below properties.
- nodeName – Name of the node object
- nodeValue – value of the node object
- parentNode – Parent node of the current node object
- childNodes – returns the collection of all child nodes
- firstChild – first child node of current node object
- lastChild – last child node of current node object
- nextSibling – next adjacent node of current node object
- previousSibling – previous adjacent node of current node object
document node is the most important node object in HTML DOM.
Getting information of document
- document.head – returns the head element node of the document.
- document.body – returns element node representing body of the document.
- document.body.innerHTML – returns the string representation of the body of the document.
- document.documentElement – returns the element node object of the document.
- document.documentElement.innerHTML – returns the HTML of the entire document.
Creating nodes
We can create nodes using below methods of document object.
- createTextNode – This is used to create a text node
- createElement – This is used to create an element node
- createAttribute – This is used to create an attribute node
- createComment – This is used to create a comment node
Adding, removing, replacing nodes
You can use below methods to add and remove nodes.
- appendChild
- insertBefore
- removeChild
- replaceChild
Document nodes properties
- document.getElementById – This method will return the element with given id
- document.getElementsByTagName – This method will return all elements with given tag name.
- document.getElementsByClassName – This method will return all elements with given class name.
- document.querySelectorAll – This method will return all elements with given CSS selector.
Below example prints the count of “table” tags.
console.log (document.getElementsByTagName(“table”).length)
Below example prints the innerHTML of the first “table” .
console.log (document.getElementsByTagName(“table”)[0].innerHTML)
We can also use below properties of document to directly access certain element nodes.
- document.head
- document.body
- document.forms – This property gets you the list of all form elements in the document.
- document.images – This property gets you the list of all image (<img> ) elements in the document.
- document.scripts
- document.title – This property will get you the title of current document
- document.URL – This property will get you the URL of current document
- document.links – This property returns all links in the page(“<a>” tag elements that have a href attribute defined)
- document.anchors – This property will get you all the anchor elements (“<a>” tag elements that have a name attribute defined)
- document.embeds
- document.readyState
- document.referrer – This property gets you the URL that loaded current document.
- document.domain – This property is used to get the domain of the server (website name).
- document.cookie – This property is used to get or set the cookies.
- document.doctype
- document.baseURI
- document.lastModified – This property gets you the last modification time stamp of the document.
- document.write – This method allows you to write the document.
You can use below code to get all properties of the document object.
for (p in document) {x = x + ” ” + p }
console.log (x)
Element nodes properties
Element node can have different properties based upon type of element. For example – form element has a “action” property. But “select” element does not have such property.
Common properties and methods applicable to all types of elements
List of properties
- nodeType
- nodeName
- ownerDocument
- parentNode
- parentElement
- childNodes
- firstChild
- lastChild
- nextSibling
- previousSibling
- hasChildNodes
- name
- target
- noValidate
- length
- title
- hidden
- tabIndex
- accessKey
- spellCheck
- contentEditable
- offsetParent
- offsetTop
- style
- innerText
- outerText
- tagName
- id
- className
- attributes
- innerHTML
- outerHTML
- scrollTop
- clientTop
- clientLeft
- clientWidth
- clientHeight
- children
- firstElementChild
- lastElementChild
- childElementCount
- hasAttributes
list of methods
- setAttribute
- checkValidity
- getAttributeNode
- insertAdjancentElement
- insertAdjancentText
- insertAdjancentHTML
- removeAttribute
- getAttribute
- scrollIntoView
- before
- after
- prepend
- append
- getRootNode
- contains
- insertBefore
- appendChild
- removeChild
- addEventListener
- removeEventListener
- dispatchEvent
- click
- focus
- blur
Properties and methods applicable to only form Element
- elements – This property returns all elements inside form.
- action – This property returns the action attribute’s value. action attribute is usually used to specify the file (.php, .asp, .jsp etc) on the web server to be executed.
- acceptCharset – sets or gets the character set accepted by form.
- encType – This property gets or sets the encoding type of the form.
- autocomplete
- method – This property returns the HTTP method (Get or Post) to be used to send the HTTP request.
- submit – This method is used to submit the form to server.
- reset – This method is used to reset a form.
- target – This property lets you specify the target window (_self or _blank etc ) where response should be displayed.
Properties and methods applicable to only input type (text boxes) Elements
- alt
- accept
- autofocus
- disabled –
- checked
- height
- list
- minLength
- min
- max
- maxLength
- multiple
- required
- pattern
- readOnly
- placeHolder
- size
- src
- step
- type
- defaultValue
- value
- width
- labels
- translate
We can access the form in which specific element exists by using below line of code.
document.getElementById(“myid”).form
Properties and methods applicable to only drop down (select) elements
- options – returns the collection of all option elements
- size – sets or gets the size of drop down
- multiple – This property (when set to true) will let you select multiple options in the list. This is usually used with size property.
- selectedIndex – sets or gets the index of the selected option in the drop down
- remove – This method lets you remove the option specified using index.
Below line of code shows how to display the selected option in the drop down.
<dropdownElement>.options[dropdown.selectedIndex].innerText
Properties and methods applicable to table
- caption
- tHead, createTHead
- tFoot, createTFoot
- tBody, createTBody
- rows
- rows[i].cells – Returns the collection of all cells in specific row.
- align
- border
- frame
- rules
- summary
- bgColor
- width
- cellPadding
- cellSpacing
- createCaption
- deleteCaption
- insertRow – Inserts a new row at specified index.
- deleteRow – Deletes the row from specified index.
To create the caption, you can use below syntax.
<tableElement>.createCaption().innerText= “This is the Table Caption”
Below example shows how to insert a new row in table.
var myRow = document.getElementById(“table1”).insertRow(1);
var myCell = x.insertCell(0);
myCell.innerHTML = “This will go in to first cell”;
Recent Comments