×

Dart HTML DOM

All web pages can be viewed as objects and exist within the browser window. We can access a web page using a web browser and it needs to be connected to the internet. DOM is an acronym for Document Object Model. Document object refers to an HTML document displayed in that window. The document object model contains a number of structures that refer to other objects that provide the modification of document content.

The process for accessing the contents of a document is called the Document Object Model, or DOM. Items are sorted by position. The hierarchical structure is used for organizing objects in a web document.

  1. Window – The first object in the hierarchy is the Window. It the outmost element of the object hierarchy.
  2. Document - When an HTML document loads in a window it becomes a window object. The document covers the content of the page.
  3. Elements - Mention content on a web page. Example - Title, text box etc.
  4. Nodes - These are usually elements, but are also attributes, comments, text and other forms of DOM.

Below is the hierarchy of the few important DOM objects.

Dart HTML DOM

Dart HTML DOM

We can manipulate objects and elements into DOM by using the ‘ dart : html ’ library. The console-based application cannot use the ' dart : html ' library. For work with the HTML library in the web application, we need to import ‘dart : html ’.

import ' dart : html ' ;  

Let's understand the DOM operation in the following section.

Finding DOM Elements

A document may contain multiple attributes and sometimes need to search for a specific attribute. The ‘ dart : html ’ library provides the query Selector function to search element in the DOM.

Element querySelector( String selector ) ; 

The ‘ querySelector( ) ’ function returns the first element that matches the specified group of the selector. Let's understand the following syntax.

var element1 = document.querySelector( ' .className ' ) ;  

var element2 = document.querySelector( ' #id ' ) ;  

Let's understand the following example.

Example -

We create a HTML file name index.html and also create a dart file.

< !DOCTYPE html >     
< html >   
   < head >       
      < meta charset = " utf-8 " >       
      < meta http-equiv = " X-UA-Compatible " content = " IE = edge " >       
      < meta name = " viewport " content = " width = device-width, initial-scale = 1.0 " >  
      < meta name = " scaffolded-by " content = " https://github.com/google/stagehand " >  
      < title > DemoWebApp < /title >       
      < link rel = " stylesheet " href = " styles.css " >       
      < script defer src = " main.dart " type = " application/dart " > < /script >  
      < script defer src = " packages/browser/dart.js " > < /script >   
   < /head >  
   < body >     
      < h1 >  
         < div id = " output " > < /div >   
      < /h1 >    
   < /body >   
< /html >  

Main.dart

import ' dart:html ' ;    
void main( ) {     
   querySelector( ' #output ' ).text = ' Your Dart web dom app is running ! ! ! . ' ;   
}  
Event Handling

The ‘ Dart:html ’ library provides an ‘ onClick ’ event for DOM Elements. The syntax shows how an element can handle a series of click-through events.

querySelector( ' #Id ' ).onClick.listen( eventHanlderFunction ) ;   

The ‘ querySelector( ) ’ function returns the feature to the given DOM and ‘ onClick.listen( ) ’ will take the ‘ eventHandler ’ method to be used when the click event is highlighted. The syntax for ' eventHandler ' is provided below :

void eventHanlderFunction( MouseEvent event ) { }   

Let us now take an example to understand the concept of Event Handling in Dart :

TestEvent.html

< !DOCTYPE html >   
< html >   
   < head >   
      < meta charset = " utf-8 " >   
      < meta http-equiv = " X-UA-Compatible " content = " IE = edge " >   
      < meta name = " viewport " content = " width = device-width, initial-scale = 1.0 " >   
      <meta name = " scaffolded-by " content = " https://github.com/google/stagehand " >   
      < title > DemoWebApp < /title >   
      < link rel = " stylesheet " href = " styles.css " >   
      < script defer src = " TestEvent.dart " type = " application/dart " > < /script >   
      < script defer src = " packages/browser/dart.js " > < /script >   
   < /head >  
   < body >   
      < div id = " output " > < /div >   
      < h1 >   
         < div >   
            Enter you name : < input type = " text " id = " txtName " >   
            < input type = " button " id = " btnWish " value = " Wish " >   
         < /div >   
      < /h1 >   
      < h2 id = " display " > < /h2 >  

TestEvent.dart

import ' dart:html ' ;   
void main( ) {   
   querySelector( ' #btnWish ' ).onClick.listen( wishHandler ) ;   
}    
void wishHandler( MouseEvent event ) {   
   String name = ( querySelector( ' #txtName ' )  as InputElement ).value ;   
   querySelector( ' #display ' ).text = ' Hello Mr. ' + name ;   
}  

Related Topics

Dart Static Members

Static Members in Dart Static members are the members of the class declared using the 'static' keyword. the static members have the following characteristics :  All the objects of the class having...

3 minutes read.

Dart Syntax

In the previous tutorial, we coded our first program in Dart on various platforms understanding the basic functionality of each line of code.  Let us understand the syntax for Dart language...

4 minutes read.

Builder Classes in Dart

Before understanding builder classes, let us understand about Flutter that makes the best use of the builder classes. Flutter is actually not a programming language, it is a software development...

6 minutes read.

Dart Inheritance

Inheritance is a promising concept of any programming language. It is a property by which a class inherits the property of another class. Moreover, the class deriving the properties of...

5 minutes read.

Dart Anonymous Function

Dart functions are the user defined functions that are designed to perform specific task. The use case of the functions is that they can be directly called any number of...

3 minutes read.

Dart Optional Parameters

Dart functions can have optional parameters with default values. When we create a function, we can specify parameters that calling code can provide; but if the calling code chooses not...

2 minutes read.

Dart Constants

Dart Constants are the objects or variables whose values can’t change or modify during the execution of the program. Their use case is when we want a particular value to...

2 minutes read.

Dart Break and Continue

Loop statements are used to change the normal sequence of flow of the program. Dart supports two types of loop control statements: Break StatementContinue Statement Break Statement : The ‘ break ’ statement is...

4 minutes read.

Dart Future and Stream

Before understanding Future and Stream in Dart, we need first to get familiar with two terms : Synchronous and Asynchronous Programming.  In synchronous programming, a single operation is handled at a...

9 minutes read.

Dart Types of Functions

Functions are the set of statements intended to perform a specific task. They accept some input from the user, perform the specified computation and generate the output. They are very...

4 minutes read.

Dart Symbols

Symbols data type in Dart A symbol is an object that is the representation of an operator or identifier in Dart. These are compile-time constants.  They are used in APIs that refer...

1 minute read.

Dart Generics

Dart Generics is similar to Dart collections, which are used to store homogeneous data. As we have discussed in Dart's features, it is a language with types being optional. By default,...

4 minutes read.

Assert in DART

In any programming language, resolving error is the most unwanted and tedious task. At times it becomes very difficult to find the error in the large code. Dart offers an...

3 minutes read.

Dart lists data type

The most important data type in any programming language is an ordered list of elements, an array. Dart lists resemble JavaScript array lists. Example,               var...

7 minutes read.

Dart Built-in Data Types

A data type defines the type of value a variable can hold, such as integer, double, or string.  Dart supports two data types:  1. Built-in data types : These are the data...

1 minute read.

Dart Queues

A queue is a user-defined collection of data. It is based on the FIFO principle ( First In First Out ) which implies that the element to be inserted first,...

3 minutes read.

Dart Installation Guide

There are various ways of compiling and running an application created in Dart, either by compiling the Dart code to JavaScript using the Dart2js tool or by running on the...

3 minutes read.

Dart Operators Precedence and Associativity

Precedence of operators determines the order in which the operators are evaluated if they are grouped together in a sentence. If two operators share an operand then the one with...

5 minutes read.

Dart Tutorial

Dart is an open-source, structured programming language developed by Google. It is a high-level programming language that emerged in 2011, but its stable version emerged in 2017. It is largely used...

4 minutes read.

Dart Loops

Looping refers to repeating or reusing the same lines of code repeatedly until a particular test condition evaluates to true. It is also known as iterating.  It is effectively used when...

4 minutes read.