domhelper

DOM Helper

Document Object Model (DOM) Helper is a very simple library of functions to help students new to programming develop web apps using a small set of easy to use functions. I developed this for use with my high school computer science classes. My students only get one quick introductory unit in HTML/CSS and the rest of the program is focused on programming, primarily in JavaScript. The idea behind this project was to allow students to use the UI capabilities of web browsers before they have technical knowledge necessary to understand how use the DOM.

Table of Contents

Setup

  1. Create a basic html document with a <body> element.
  2. Add this script tag before your own script tag <script defer src="https://pueblocs.org/domhelper/domhelper.js"></script> (See the example below)
     <script defer src="https://pueblocs.org/domhelper/domhelper.js"></script>
     <script defer src="script.js"></script>
    
  3. Use the DOM Helper functions in your script.js file

     createElement("h1", "heading");
     setWidth(getWidth(), "heading");
     setText("Welcome", "heading");
     setColor("orange", "heading");
     setStyle("text-align", "center", "heading");
    
     createButton("start-button", "Start");
     setX(getWidth() / 2 - getWidth("start-button") / 2, "start-button");
     setY(getHeight() / 2 - getHeight("start-button") / 2, "start-button");
     addClickEvent("start-button", start);
    
     function start() {
       console.log("Woohoo");
     }
    

Element Creation

By default, elements are created with position: absolute and must be positioned on the screen using x- and y-coordinates which correspond to the left and top of the element

createButton(id, text, x, y)

Creates a button element with the given id and text

createCheckBox(id, x, y)

Creates a square check box with the given id

createCircle(x, y, r, color, id)

Creates a circle with the specified properties. The circle is located by its center

createElement(type, id, x, y)

createImg(url, id, x, y)

createRect(x, y, w, h, color, id)

createRadioButton(id, group, x, y)

Creates a round radio button with the given id as a part of the given group. Only one radio button within a group can be checked at a time

createTextBox(id, x, y)

Creates a textbox to provide a place for the user to enter text

Size functions

setHeight(value, id)

getHeight(id)

Returns the width of the element as a number type in px

setWidth(value, id)

getWidth(id)

Returns the width of the element as a number type in px

Position and movement functions

setX(value, id)

Moves the element with the given id to the given x-coordinate value

getX(id)

Returns the x-coordinate of the element with the given id in px as a number type

changeXBy(value, id)

Moves the element along the x-axis by the given amount (adds the value to the current x-coordinate)

setY(value, id)

Moves the element with the given id to the given y-coordinate value

getY(id)

Returns the y-coordinate of the element with the given id in px as a number type

changeYBy(value, id)

Moves the element along the y-axis by the given amount (adds the value to the current y-coordinate)

move(value, id)

Moves the element along its angle of rotation by the given amount (moves forward in the direction it’s pointing)

Rotation and direction functions

rotate(degrees, id)

Rotates the element by the given amount from its current angle of rotation

setRotation(degrees, id)

Sets the angle of rotation to the given value

getRotation(id)

Returns the angle of rotation of the specified element in deg as a number type

Content functions

setText(text, id)

Set the text content (inner HTML) of the element with the specified id

getText(id)

Returns the text (inner HTML) of the element with the specified id

setImage(src, id)

Sets the source for an image element

getImage(id)

Returns the URL of the src attribute for an image element

attach(id, parentId)

Attaches the element with id to be a child of the element with parentId

remove(id)

Removes the element with id from the layout

Navigation

Opens the specified URL in the browser window

CSS style functions

setColor(color, id)

Sets the color (background color) of the element

getColor(id)

Returns the color (background color) of the element

setTextColor(color, id)

Sets the text color of the element

getTextColor(id)

Returns the text color of the element

setStyle(property, value, id)

Sets the specified CSS style property

getStyle(property, id)

Returns the value of the specified CSS style property

hide(id)

Hides the element with the given id making it invisible

hideAll()

Hides all the elements in the layout

show(id)

Shows the element with the given id making it visible

showAll()

Makes all the elements in the layout visible

isVisible(id)

Returns true if the element with the given id is visible, otherwise false

Sound

playSound(url)

Plays the sound located a url

Forms and Input Elements

getInputValue(id)

Returns the value entered by the user in a textbox as a string

setInputValue(value, id)

Sets the input value to the given value

clearInputValue(id)

Clears the value from the input element

isChecked(id)

Returns true if the checkbox or radio button with the given id is checked

Events

addClickEvent(id, f)

Assigns a function to call when the element is clicked

removeClickEvent(id, f)

Removes the click event from the element with the given id

addEventListener(type, f, id)

Assigns a function to call when the specified event happens

addEventListener("keydown", keyPressed);

function keyPressed(event) {
  if (event.code == "ArrowLeft") {
    console.log("The left arrow was pressed");
  }
}

isTouching(id1, id2)

Returns true if the two elements are touching

Random

getRandomInt(from, to)

Returns a random integer between from and to inclusive

getRandomColor()

Returns a random rgb() color string for use in CSS style property

Animation

To setup a project with animation, you must define a mainLoop() or mainLoop(timeStamp) function.

function mainLoop(timeStamp) {
  // Update state of animated elements
}

hideControls()

Hides the animation controls

showControls()

Shows animation controls with built-in start and stop buttons

startAnimation()

Starts the animation loop which will repeatedly call your mainLoop(time) function.

stopAnimation()

Stops the animation loop