The Wayback Machine - https://web.archive.org/web/20240620234557/https://www.geeksforgeeks.org/html-document-object-model-and-dart-programming/
Open In App

HTML Document Object Model and Dart Programming

Last Updated : 08 Mar, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

We all know that dart is a type of javascript. So we can manipulate data inside HTML pages with the help of dart in a similar fashion as we can do with javascript.

In HTML DOM (Document Object Model) every webpage is on a window, so it is considered as an object. The hierarchical model that follows is as follows:

Dart provides the user to manipulate these elements in an HTML page with the help of dart:html library. To use a dart program inside HTML you have to import this library in your code.

Syntax:

import 'dart:html';

To access the elements inside the page we make use of query selector function to manipulate the element.
Syntax:

Element querySelector(String selectors);

The String selector can be either class name (.class_name), id (#id), html element (h1, p, …), attribute type (input[type=”button”], …) or either asterisk (*).

There are two ways to use practice dart in your webpage:

  1. Online on Dartpad.
  2. In your system.

For the first one, you don’t have to install dart in your system.
Steps:

  1. Open Dartpad in new window.
  2. Now click on newpad button.
  3. Select Dart language and also enable Html button and hit the create button.

In this way, you will get the place to work on the dart and embed it with an HTML page. The other way to do so requires dart-SDK inside your system.

To do so you can follow this website.

Example 1: Creating a simple dart-Html page.

HTML


We will be linking dart and css files with our HTML page. Apart from that we also have to link dart javascript so that it may convert dart file to browser understandable javascript.




<!DOCTYPE html>  
<html>
   <head>
      <title>Geeks for Geeks</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 id="header"></h1>
  </body>
</html>


Dart


This is main.dart




// Importing dart HTML library
import 'dart:html';
 
void main()
{
    // Calling the element with id header and setting its value dynamically
    var header = querySelector('#header');
    header.text = "Geeks For Geeks!";
}


CSS


This is styles.css




// Setting CSS style
body {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  position: absolute;
  width: 100%;
  height: 100%;
  background-color: whitesmoke;
}
 
h1 {
  color: ForestGreen;
  font-family: Arial, Helvetica, sans-serif;
}



Output:
The output screen looks like:

Understanding the control flow:

Example 2: Creating a string concatenation program.

HTML




<!DOCTYPE html>  
<html>
   <head>
      <title>Geeks for Geeks</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>Geeks For Geeks</h1>
     <div class="gfg">
       Enter First String: <input type = "text" id = "first_name">
       <br><br>
       Enter Second String:  <input type = "text" id = "second_name">
       <br><br>
       Enter Third String:  <input type = "text" id = "third_name">
       <br><br>
       <input type = "button" id = "concat" value="Concat">
     </div>
     <h3 id="output"></h3>
  </body>
</html>


Dart


This is main.dart




// Importing dart HTML library
import 'dart:html';
 
void main()
{
    // This statement will call geeksForGeeks function when concat
    // button is clicked...
    querySelector('#concat').onClick.listen(geeksForGeeks);
}
 
// Function taking all the three strings,
// concating them and displaying inside h3 tag...
void geeksForGeeks(MouseEvent e)
{
    String s1 = (querySelector('#first_name') as InputElement).value;
    String s2 = (querySelector('#second_name') as InputElement).value;
    String s3 = (querySelector('#third_name') as InputElement).value;
 
    querySelector('#output').text = s1 + ' ' + s2 + ' ' + s3;
}


CSS


This is styles.css




body {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  position: absolute;
  width: 100%;
  height: 100%;
  background-color: whitesmoke;
}
 
h1 {
  color: ForestGreen;
  font-family: Arial, Helvetica, sans-serif;
}
 
.gfg {
  color: red;
  font-family: Arial, Helvetica, sans-serif;
}
 
#output{
  background-color: pink;
  color: Red;
}



Output:

When You hit the concat button then the below text is generated dynamically by concatenation of above strings.


Similar Reads

Flutter - Convert JSON to Model Class in Dart
Many times you are receiving API data but it is difficult to create a model class manually for some data that are too big. So we can make a model with just 2 steps. Let's take sample data for which we want to create a model class. { "userId": 1, "id": 1, "title": "delectus aut autem", "completed": false }Step-by-Step Implementation Step 1: Just Cop
2 min read
Flutter - Create a Dart Model using Freezed Package
In this article, we will learn how to create a dart model automatically with a sample of code using the freezed package. If you are making a large-scale app. You cannot write a model class file for every data you receive from the backend. So here's a simple solution for it where this model will automatically generate you just need to add sample dat
3 min read
Hybrid Programming using Python and Dart
Hybrid programming is a programming approach that combines different programming paradigms or styles in a single application. It involves using elements from both object-oriented programming and functional programming, for example, or mixing imperative programming with declarative programming. It is possible to use both Dart and Python in a single
4 min read
Dart Programming - Map
In Dart programming, Maps are dictionary-like data types that exist in key-value form (known as lock-key). There is no restriction on the type of data that goes in a map data type. Maps are very flexible and can mutate their size based on the requirements. However, it is important to note that all locks (keys) need to be unique inside a map data ty
3 min read
Introduction to Dart Programming Language
Dart is an open-source programming language originally developed by Google. It is meant for both the server side as well as the user side. The Dart SDK comes with its compiler - the Dart VM and a utility dart2js which is meant for generating the Javascript equivalent of a Dart Script so that it can be run on those sites also which don't support Dar
4 min read
Dart Programming - If Else Statement (if , if..else, Nested if, if-else-if)
Decision-making statements are those statements that allow the programmers to decide which statement should run in different conditions. There are four ways to achieve this:  if Statementif-else Statementelse-if LadderNested if Statementif StatementThis type of statement simply checks the condition and if it is true the statements within it are exe
3 min read
Dart - Super and This keyword
Super Keyword in Dart: In Dart, super keyword is used to refer immediate parent class object. It is used to call properties and methods of the superclass. It does not call the method, whereas when we create an instance of subclass than that of the parent class is created implicitly so super keyword calls that instance. Advantages of super keyword:
3 min read
Dart - Loop Control Statements (Break and Continue)
Dart supports two types of loop control statements: Break Statement Continue Statement Break Statement: This statement is used to break the flow of control of the loop i.e if it is used within a loop then it will terminate the loop whenever encountered. It will bring the flow of control out of the nearest loop. Syntax: break; Example 1: Using break
4 min read
Instance and class methods in Dart
Dart provides us with the ability to create methods of our own. The methods are created to perform certain actions in class. Methods help us to remove the complexity of the program. It must be noted that methods may and may not return any value and also it may or may not take any parameter as input. Methods in a class can be either an object method
2 min read
Dart - Finding Minimum and Maximum Value in a List
In Dart we can find the minimum and maximum valued element present in the given list in seven ways: Using for loop to find the largest and smallest element.Using sort function to find the largest and smallest element.Using forEach loop to find the largest and smallest element.Using only reduce method in dart to find the largest and smallest element
5 min read
Article Tags :