Copy to clipboard

navigator.clipboard.writeText(content);

Open url with target

window.open(url, target);

JS_OpenURL_WithTarget.mpk

window.print();

JS_Print.mpk

Toggle class on element

const element = document.querySelector(querySelector);
if (element) {
    if (element.classList.contains(className)) {
        element.classList.remove(className);
    }
    else {
        element.classList.add(className);
    }
    return true;
}
else return false;

JS_ToggleClassOnElement.mpk

Select all text on input focus

Set this JavaScript action on the OnChange nanoflow

const activeElement = document.activeElement;
if (activeElement.tagName === "INPUT") activeElement.select();

JS_Input_SelectOnFocus.mpk

Scroll to element

// BEGIN EXTRA CODE
function scrollToElement(targetSelector, block, inline) {
	const elements = document.querySelectorAll(targetSelector);
    if (elements) {
        const element = elements[elements.length -1];
		if (element){
			console.debug(element);
			element.scrollIntoView({
				behavior: "smooth",
				block: block,
				inline: inline
			});
			return true;
		}
	}
	return false;
}
// END EXTRA CODE

if (scrollToElement(targetSelector, block, inline)) return;

//Retry because sometimes the DOM element is not loaded yet. 
console.log("no element found");
setTimeout(function() {
    scrollToElement(targetSelector, block, inline);
},200);

JS_ScrollToElement.mpk

Scroll to error

const validationMessage = document.querySelector(".mx-validation-message");
if(validationMessage) {
    scrollIntoView(validationMessage);
    return;
}
const alert = document.querySelector(".alert.alert-danger");
if(alert) {
    scrollIntoView(alert);
    return;
}
console.warn("No validation message found.");

JS_ScrollToError.mpk