×

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 Type Test Operators

Type Test Operators in Dart These operators are used to check the types of expressions at runtime. Dart is a typed language, and we often want to assert that a value...

1 minute read.

Dart Equality and Relational Operators

Dart provides the functionality of checking the relationship between the values or values within the variables. These operators return the resultant value as Boolean, i.e., either ‘true’ or ‘false’. Some important points...

3 minutes read.

Dart Arithmetic Operators

Arithmetic Operators comprises of all the operators that are used to perform arithmetic operations such as addition, subtraction, multiplication, division, etc. They are binary operators that work upon two operands. Consider A...

2 minutes read.

Lexical Scope and Closure

Lexical Scope : Lexical scope is a very important term in any programming language. It defines the scope of the variable based on the block it is contained inside. According to...

2 minutes read.

Type System in Dart

Dart language is a type safety enabled language that uses static and dynamic type checking to match the value of the variable with its data type at the compile-time. This...

4 minutes read.

Dart Logical Operators

Logical operators are the operators that combine two or more conditions, and accordingly return a Boolean value : true or false. Assume the value of variable A is 25 and B...

2 minutes 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 Enumerations

Enumeration data type in Dart In simple terms, Enumerations are referred to as named constant values. Constant values are declared as enumerations using the ‘enum’ keyword.  Implementation =  enum enum_name {    ...

2 minutes read.

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...

3 minutes read.

Dart If-else statement

In the previous section, we studied about if statements in detail. If – block is executed when the condition evaluates to true, else if the condition evaluates to false, then...

1 minute read.

Dart If-else-if statement

If - else - if statement enables to check the set of test expressions that evaluate to Boolean True or False, and based on this true or false the particular...

2 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 Variables

Variables are used to store the value of a particular data type referred to in the whole program by a name or identifier. They refer to a memory location as...

5 minutes read.

Dart Maps

Map is an object-based data structure that associates keys and values that can be of any data type. Every key can occur only once, while the values can be used multiple...

5 minutes read.

Dart URIs

The Uri class supports encoding and decoding of strings with the help of functions to be used in URIs ( which may also be known as URLs ). These functions...

6 minutes read.

Dart Switch Case Statement

In the case of if-else statements, we prefer not to use the if-else ladder when there are many test conditions to be evaluated. In such a situation, it is preferable...

4 minutes read.

Soundness in Dart

What is soundness? Soundness ensures that the program never comes across any invalid state that may lead to abrupt termination of the program. As its name suggests it ensures perfect type...

4 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.

Golang vs Dart

Go is the procedural programming language. It was founded in 2007 by Robert Griesemer, Rob Pike, and Ken Thompson at Google but was launched in 2009 as the language of...

2 minutes read.