JavaScript DOM Click Event

A picture of a large light switch near a doorway to signify JavaScript DOM Event

Create DOM Click Event In JavaScript

To demonstrate, DOM event and styling with JavaScript, a new window will be opened in a new web browser window or tab.

Multiple windows can be opened and reused assuming that JavaScript is not disabled. JavaScript can be used to create events, inline styles or a internal or external styles.

JavaScript open() method can take optional parameters for the name, URL and window dimensions. Inline style attributes are inside HTML elements, Internal styles use a style element. The DOM event for this tutorial is the click method. External Event styles use the link element in the head section on an HTML document.

Common Syntax Of DOM Event Click In JavaScript

Glossary:

URL

Page to open.

Name

Target attribute or name of window.

Specs

Comm-separated list of items such as dimensions, location and scrollbars.

CSS

Cascading Style Sheets inherit properties and methods from the parent.

Event

Action triggered by user interaction of browser occurrence.

Common Methods For DOM Event

Ojamboshop.com Learning JavaScript DOM Event
Name Description Example
open() Create a new browser window. window.open();
style=”property:value” Inline Event. style=”color:blue;”
onclick Trigger a function when an element is clicked. element.onclick = myFunc;
Name Description Example

JavaScript DOM Event Snippet

// OjamboShop.com Learning JavaScript DOM Click Event Tutorial

// DOM Click Event
let newWindow = window.open("", null, "height=200,width=400,status=yes,toolbar=no,menubar=no,location=no");  

// New Window Content
let p = newWindow.document.createElement("p");
p.innerHTML = `
Be sure to check out <a href="https://ojamboshop.com">OjamboShop.com</a> <span>for the courses and to leave a donation click <a href="https://ojambo.com/donate">here</a></span>`;
let div = newWindow.document.createElement("div");
let btn = newWindow.document.createElement("button");
btn.innerHTML = `Message`;
btn.setAttribute('onclick', 'document.querySelector("div").innerHTML += "<br />The time-stamp is now: " + Date();');
let style = newWindow.document.createElement("style");
style.innerHTML = `span { color: red; font-weight: bold; }`;
let meta = newWindow.document.createElement("meta");
meta.httpEquiv = 'Content-Security-Policy';
meta.content = `default-src 'self'; script-src 'self' 'unsafe-inline'`;
newWindow.document.head.appendChild(meta);
newWindow.document.body.appendChild(p);
newWindow.document.body.appendChild(div);
newWindow.document.body.appendChild(btn);
newWindow.document.body.appendChild(style);
newWindow.document.querySelectorAll('a').forEach(function(elem) {
	elem.addEventListener('click', function(evt) {
		evt.preventDefault();
		return confirm('Are you sure?');
	});
});
newWindow.document.querySelector('button').addEventListener('click', function(event) {
	newWindow.document.querySelector('div').innerHTML += '<br />The time-stamp is now: ' + Date();
});

JavaScript DOM Event Code
OjamboShop.com Web IDE JavaScript DOM Click Event Code

JavaScript DOM Event Result
OjamboShop.com Web IDE Displaying JavaScript DOM Click Event Result


Usage

You can use any IDE or text editor and the web browser to compile and execute JavaScript code. For this tutorial, the OjamboShop.com Learning JavaScript Course Web IDE can used to input and compile JavaScript code for the open window DOM Click Event.

Open Source

JavaScript follows the ECMAScript standard and is licensed under the W3C Software License by web browser vendors and runtime environment vendors. This allows commercial use, modification, distribution, and allows making derivatives proprietary.

Live Stream

Every Friday, you can join a live stream and ask questions. Check Ojambo.com for details and instructions.

Learn Programming Courses:

Get the Learning JavaScript Course for your web browser on any device.

OjamboShop.com Learning JavaScript Course
OjamboShop.com Learning JavaScript Interactive Online Course

Learn Programming Books:

Learning Javascript Book is available as Learning JavaScript Paperback or Learning JavaScript Ebook.

OjamboShop.com Learning JavaScript Book Announcement
OjamboShop.com Learning JavaScript Ebook And Paperback Announcement

Conclusion:

JavaScript makes it easy to create DOM click event styled content for new windows. New windows can be opened in the same window, as a new window or new tab.

If you enjoy this article, consider supporting me by purchasing one of my WordPress Ojambo.com Plugins or programming OjamboShop.com Online Courses or publications at Edward Ojambo Programming Books or become a donor here Ojambo.com Donate

References:

Leave a Reply

Your email address will not be published. Required fields are marked *