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.
<body>
element.<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>
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");
}
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
Creates a button element with the given id and text
id
the id to give the new button elementtext
optional the text to display on the buttonx
optional the x-coordinate to position the element in px
y
optional the y-coordinate to position the element in px
Creates a square check box with the given id
id
the id to give the new button elementx
optional the x-coordinate to position the element in px
y
optional the y-coordinate to position the element in px
Creates a circle with the specified properties. The circle is located by its center
x
the x-coordinate to position the element in px
y
the y-coordinate to position the element in px
r
the radius of the circle in px
color
optional the color of the circle (background)id
optional the id to give the elementtype
the tag name of the type of element to createid
optional the id to give the new elementx
optional the x-coordinate to position the new element at in px
y
optional the y-coordinate to position the new element at in px
url
the file path or web address for the imageid
optional the id to give the new elementx
optional the x-coordinate to position the element at in px
y
optional the y-coordinate to position the element at in px
x
the x-coordinate to position the element at in px
y
the y-coordinate to position the element at in px
w
the width of the rectangle in px
h
the height of the rectangel in px
color
optional the color of the rectangle (background)id
optional the id to give the new elementCreates 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
id
the id to give the new button elementgroup
the group of radio buttons to which this button should belongx
optional the x-coordinate to position the element in px
y
optional the y-coordinate to position the element in px
Creates a textbox to provide a place for the user to enter text
id
the id to give the textbox elementx
the x-coordinate to position the element at in px
y
the y-coordinate to position the element at in px
value
the size in px
id
optional the id of the element to change the height of. If no id is specified, it will set the height of the windowReturns the width of the element as a number type in px
id
optional the id of the element to get the height of. If no id is specified, it will return the height of the windowvalue
the size in px
id
optional the id of the element to change the width of. If no id is specified, it will set the width of the windowReturns the width of the element as a number type in px
id
optional the id of the element to get the width of. If no id is specified, it will return the width of the windowMoves the element with the given id to the given x-coordinate value
value
the new x-coordinate for the element in px
id
the id of the elementReturns the x-coordinate of the element with the given id in px
as a number type
id
the id of the elementMoves the element along the x-axis by the given amount (adds the value to the current x-coordinate)
value
the amount in px
to add the the current x-coordinateid
the id of the elementMoves the element with the given id to the given y-coordinate value
value
the new x-coordinate for the element in px
id
the id of the elementReturns the y-coordinate of the element with the given id in px
as a number type
id
the id of the elementMoves the element along the y-axis by the given amount (adds the value to the current y-coordinate)
value
the amount in px
to add the the current y-coordinateid
the id of the elementMoves the element along its angle of rotation by the given amount (moves forward in the direction it’s pointing)
value
the amount in px
to moveid
the id of the elementRotates the element by the given amount from its current angle of rotation
degrees
the amount in deg
to rotate the element byid
the id of the elementSets the angle of rotation to the given value
degrees
the new angle in deg
for the elementid
the id of the elementReturns the angle of rotation of the specified element in deg
as a number type
id
the id of the elementSet the text content (inner HTML) of the element with the specified id
text
the text to displayid
the id of the elementReturns the text (inner HTML) of the element with the specified id
id
the id of the elementSets the source for an image element
src
the web address or file for the image sourceid
the id of an image elementReturns the URL of the src
attribute for an image element
id
the id of an image elementAttaches the element with id
to be a child of the element with parentId
id
the id of the child elementparentId
optional the id of the parent or container element. If no parent is specified it will be attached to the <body>
elementRemoves the element with id
from the layout
id
the id of the element to removeOpens the specified URL in the browser window
url
the URL (web address or file) to navigate toSets the color (background color) of the element
color
the new color of the elementid
optional the id of the element or, if omitted, the body element (window)Returns the color (background color) of the element
id
optional the id of the element or, if omitted, the body element (window)Sets the text color of the element
color
the new color of the textid
optional the id of the element or, if omitted, the body element (window)Returns the text color of the element
id
optional the id of the element or, if omitted, the body element (window)Sets the specified CSS style property
property
the name of the CSS property to set. Property names can be passed with hyphens, e.g. text-align
, or in camelCase, e.g. textAlign
value
the new value to set for the propertyid
the id of the element on which to set the propertyReturns the value of the specified CSS style property
property
the name of the CSS property to set. Property names can be passed with hyphens, e.g. text-align
, or in camelCase, e.g. textAlign
id
the id of the element from which to get the property valueHides the element with the given id making it invisible
id
the id of the element to hideHides all the elements in the layout
Shows the element with the given id making it visible
id
the id of the element to showMakes all the elements in the layout visible
Returns true if the element with the given id is visible, otherwise false
id
the id of the elementPlays the sound located a url
url
the URL or file name of the sound to playReturns the value entered by the user in a textbox as a string
id
the id of the input elementSets the input value to the given value
value
the value to set for the input elementid
the id of the input elementClears the value from the input element
id
the id of an input elementReturns true if the checkbox or radio button with the given id is checked
id
the id of the checkbox or radio buttonAssigns a function to call when the element is clicked
id
the id of the elementf
the name of the function to call when the element is clickedRemoves the click event from the element with the given id
id
the id of the elment with the click eventf
the name of the function associated with the click eventAssigns a function to call when the specified event happens
type
the type of event to listen for. Some common ones are "click"
,"keydown"
, "keyup"
, "mousedown"
, and "mouseup"
. For a complete list see W3Schools HTML Eventsf
the name of the function to call when a key is pressedid
optional the id of the HTML element to detect events for. If no id is specified, it will listen for events on the entire documentaddEventListener("keydown", keyPressed);
function keyPressed(event) {
if (event.code == "ArrowLeft") {
console.log("The left arrow was pressed");
}
}
Returns true if the two elements are touching
id1
the id of an elementid2
the id of another elementReturns a random integer between from
and to
inclusive
from
the starting value for the rangeto
the ending value of the rangeReturns a random rgb()
color string for use in CSS style property
To setup a project with animation, you must define a mainLoop()
or mainLoop(timeStamp)
function.
function mainLoop(timeStamp) {
// Update state of animated elements
}
Hides the animation controls
Shows animation controls with built-in start and stop buttons
Starts the animation loop which will repeatedly call your mainLoop(time)
function.
Stops the animation loop