Skip to content

Instantly share code, notes, and snippets.

@lucasrmagalhaes
Last active March 24, 2022 20:34
Show Gist options
  • Save lucasrmagalhaes/9b660c4b785bdc3adbd28a70dc0d127e to your computer and use it in GitHub Desktop.
Save lucasrmagalhaes/9b660c4b785bdc3adbd28a70dc0d127e to your computer and use it in GitHub Desktop.
JavaScript - Códigos
/** Gera a Data Atual */
let data, dia, mes, ano, dataAtual;
data = new Date();
dia = String(data.getDate()).padStart(2, '0');
mes = String(data.getMonth() + 1).padStart(2, '0');
ano = data.getFullYear();
dataAtual = dia + '/' + mes + '/' + ano;
/** DOM Methods */
<p id="exemplo"></p>
document.getElementById("exemplo").innerHTML = "Hello World!";
/** DOM Document - Finding HTML Elements */
document.getElementById('id') // Find an element by element id
document.getElementsByTagName('tagName') // Find elements by tag name
document.getElementsByClassName('className') // Find elements by class name
/** DOM Document - Changing HTML Elements */
element.innerHTML = new html content // Change the inner HTML of an element
element.attribute = new value // Change the attribute value of an HTML element
element.style.property = new style // Change the style of an HTML element
element.setAttribute(attribute, value) // Change the attribute value of an HTML element
/** DOM Document - Adding and Deleting Elements */
document.createElement(element) // Create an HTML element
document.removeChild(element) // Remove an HTML element
document.appendChild(element) // Add an HTML element
document.replaceChild(new, old) // Replace an HTML element
document.write(text) // Write into the HTML output stream
/** DOM Document - Adding Events Handlers */
document.getElementById(id).onclick = function() { // Adding event handler code to an onclick event
code
}
/** DOM Document - Finding HTML Objects */
document.anchors // Returns all <a> elements that have a name attribute
document.applets // Deprecated
document.baseURI // Returns the absolute base URI of the document
document.body // Returns the <body> element
document.cookie // Returns the document's cookie
document.doctype // Returns the document's doctype
document.documentElement // Returns the <html> element
document.documentMode // Returns the mode used by the browser
document.documentURI // Returns the URI of the document
document.domain // Returns the domain name of the document server
document.domConfig // Obsolete.
document.embeds // Returns all <embed> elements
document.forms // Returns all <form> elements
document.head // Returns the <head> element
document.images // Returns all <img> elements
document.implementation // Returns the DOM implementation
document.inputEncoding // Returns the document's encoding (character set)
document.lastModified // Returns the date and time the document was updated
document.links // Returns all <area> and <a> elements that have a href attribute
document.readyState // Returns the (loading) status of the document
document.referrer // Returns the URI of the referrer (the linking document)
document.scripts // Returns all <script> elements
document.strictErrorChecking // Returns if error checking is enforced
document.title // Returns the <title> element
document.URL // Returns the complete URL of the document
/** DOM Elements */
const element = document.getElementById("intro"); // Finding HTML elements by id
const element = document.getElementsByTagName("p"); // Finding HTML elements by tag name
const x = document.getElementsByClassName("intro"); // Finding HTML elements by class name
const x = document.querySelectorAll("p.intro"); // Finding HTML elements by CSS selectors
const x = document.forms["frm1"]; // Finding HTML elements by HTML object collections
let text = "";
for (let i = 0; i < x.length; i++) {
text += x.elements[i].value + "<br>";
}
document.getElementById("demo").innerHTML = text;
/** DOM HTML */
// Changes the content of a <p> element
<p id="p1">Hello World!</p>
document.getElementById("p1").innerHTML = "New text!";
// Changes the content of an <h1> element
<h1 id="id01">Old Heading</h1>
const element = document.getElementById("id01");
element.innerHTML = "New Heading";
// Changing the Value of an Attribute
<img id="myImage" src="smiley.gif">
document.getElementById("myImage").src = "landscape.jpg";
// Dynamic HTML content
document.getElementById("demo").innerHTML = "Date : " + Date();
/**
* Write directly to the HTML output stream.
* Never use document.write() after the document is loaded. It will overwrite the document.
*/
document.write(Date());
/** DOM Forms */
// JavaScript Form Validation
function validateForm() {
let x = document.forms["myForm"]["fname"].value;
if (x == "") {
alert("Name must be filled out");
return false;
}
}
<form name="myForm" action="/action_page.php" onsubmit="return validateForm()" method="post">
Name: <input type="text" name="fname">
<input type="submit" value="Submit">
</form>
// JavaScript Can Validate Numeric Input
<input id="numb">
<button type="button" onclick="myFunction()">Submit</button>
<p id="demo"></p>
function myFunction() {
// Get the value of the input field with id="numb"
let x = document.getElementById("numb").value;
// If x is Not a Number or less than one or greater than 10
let text;
if (isNaN(x) || x < 1 || x > 10) {
text = "Input not valid";
} else {
text = "Input OK";
}
document.getElementById("demo").innerHTML = text;
}
// Automatic HTML Form Validation - required attribute prevents this form from being submitted
<form action="/action_page.php" method="post">
<input type="text" name="fname" required>
<input type="submit" value="Submit">
</form>
/**
* Data Validation
* Server side validation is performed by a web server, after input has been sent to the server.
* Client side validation is performed by a web browser, before input is sent to a web server.
*/
/**
* HTML Constraint Validation - based on:
* HTML Input Attributes
* CSS Pseudo Selectors
* DOM Properties and Methods
*/
/** Formata um número utilizando notação de ponto fixo. */
const arquivo = Number(response.arquivo);
const resultado = typeof arquivo === 'number' ? arquivo.toFixed(2) : '';
/** Remover valores duplicados */
const skills = ['js', 'js']
const removerItem - [...new Set(skills)]
console.log(removerItem)
// ['js']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment