Creating HTML Elements in Dart PRO

Dart allows you to create new HTML elements and add them to the web page. There are two ways for doing it. The following code sample shows how to do it.

The code sample includes two files. The first is the HTML file. The second is the Dart file. Once the web browser loads the HTML file it will execute the code we wrote in Dart.

<!DOCTYPE html>

<html>
  <head>
    <meta charset="utf-8">
    <title>Creating HTML Element</title>
    <link rel="stylesheet" href="samplenewelements.css">
  </head>
  <body>
    <script type="application/dart" src="samplenewelements.dart"></script>
    <script src="packages/browser/dart.js"></script>
  </body>
</html>

The Dart code includes the definition for the main() entry point function. This is the first function that will be executed when the web browser is ready to execute our code in Dart.

import 'dart:html';

void main() {
  var title = new Element.html("<h2>Creating HTML Elements Sample</h2>");
  document.body.children.add(title);
  InputElement input = new Element.tag("input");
  input.id = "username";
  input.placeholder = "enter your username";
  document.body.children.add(input);
  ButtonElement clickHereButton = new Element.tag("button");
  clickHereButton.id = "btn-click-here";
  clickHereButton.text = "Click Here";
  document.body.children.add(clickHereButton);
  Element image = new Element.html("<p><img src='mypix.png'/></p>");
  document.body.children.add(image);
}

The following video clip overviews this code sample, shows its execution and explains it.

You can find more video clips and more resources for learning Dart in my free courses web site at http://abelski.lifemichael.com.

Share:

The Visitor Design Pattern

The Visitor Design Pattern

The visitor design pattern allows us to add operations to objects that already exist without modifying their classes and without extending them.

What are Anti Patterns?

Anti Patterns

Unlike design patterns, anti patterns just seem to be a solution. However, they are not a solution and they cause additional costs.

Virtual Threads in Java Professional Seminar

Virtual Threads in Java

The use of virtual threads can assist us with improving the performance of our code. Learn how to use virtual threads effectively.

NoSQL Databases Courses, Seminars, Consulting, and Development

MongoDB Design Patterns Meetup

The use of MongoDB involves with various cases in which we can overcome performance issues by implementing specific design patterns.

image of woman and database

Record Classes in Java

Learn how to define record classes in Java, and when to use record classes in your code. Stay up to date with the new Java features.

Accessibility | Career | Conferences | Design Patterns | JavaScript | Meetups | PHP | Podcasts | Python | Self Learning

Teaching Methodologies | Fullstack | C++ | C# | CSS | Node.js | Angular | Java | Go | Android | Kotlin | Swift | Academy

Front End Development | Scala | Architectures | Cloud | Big Data | Internet of Things | Kids Learn Programming

The Beauty of Code

Coding is Art! Developing Code That Works is Simple. Develop Code with Style is a Challenge!

Skip to content Update cookies preferences