INFO 253A: Frontend Web Architecture
Kay Ashaolu
jQuery is the granddady of JavaScript Libraries
$(document).ready(function(){
$("form").submit(function(event){
var regex = /^[a-zA-Z]+$/;
var currentValue = $("#firstName").val();
if(regex.test(currentValue) == false){
$("#result")
.html('<p class="error">Not valid!</p>')
.show()
.fadeOut(1000);
// Preventing form submission
event.preventDefault();
}
});
});
React is a JavaScript library that provides functionality in such a way that it encourages the use of a new kind of framework of developing user interfaces, centralizing the data of the application in one area and asking the developer to tell the application what it should display with the data that it has.
<html>
<head>
<meta charset="utf-8" />
<link rel="stylesheet" href="../css/style.css" />
</head>
<body>
<form id="locationForm">
<label for="name">Name: </label>
<input type="text" name="name" id="name" /><br />
<label for="state">State</label>
<select name="state">
<option value="CA">CA</option>
<option value="NY">NY</option>
<option value="TX">TX</option>
</select><br />
<input type="submit" value="Save Location" />
</form>
<br />
<div id="locationList">
</div>
<script src="../js/script.js"></script>
</body>
</html>
html/index.html
.highlight {
color: red;
background-color: yellow;
}
css/style.css
/* Populate with current location */
let locationDiv = document.getElementById("locationList");
if (localStorage.curLocation != null) {
locationDiv.innerHTML = localStorage.curLocation;
}
/* Run code on submit button push */
let locationForm = document.getElementById("locationForm");
locationForm.addEventListener("submit", function(event) {
let name = locationForm.elements.namedItem("name").value;
let state = locationForm.elements.namedItem("state").value;
localStorage.curLocation = name + ": " + state;
let locationDiv = document.getElementById("locationList");
locationDiv.innerHTML = localStorage.curLocation;
locationDiv.classList.add("highlight");
/* This stops the usual function of "submit" which is to send data
to another server */
event.preventDefault();
})
js/script.js
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="icon" href="/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#000000" />
<meta
name="description"
content="Web site created using create-react-app"
/>
<link rel="apple-touch-icon" href="/logo192.png" />
<link rel="manifest" href="/manifest.json" />
<title>React App</title>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
<script src="/static/js/bundle.js"></script>
<script src="/static/js/1.chunk.js"></script>
<script src="/static/js/main.chunk.js"></script>
</body>
</html>
index.html (Autogenerated)
.highlight {
color: red;
background-color: yellow;
}
index.css
import React, { useState } from 'react';
import ReactDOM from 'react-dom';
import './App.css';
function LocationApp(props) {
const [name, setName] = useState(localStorage.getItem("curLocationName"));
const [state , setState] = useState(localStorage.getItem("curLocationState"));
const [className, setClassName] = useState("");
const handleSubmit = (event) => {
event.preventDefault();
const name = event.target.name.value;
const state = event.target.state.value
setName(name);
localStorage.setItem("curLocationName", name);
setState(state);
localStorage.setItem("curLocationState", state);
setClassName("highlight");
}
index.js
return (
<div>
<form onSubmit={handleSubmit}>
<label for="name">Name: </label>
<input
type="text"
name="name"
id="name" />
<br />
<label for="state">State</label>
<select name="state">
<option value="CA">CA</option>
<option value="NY">NY</option>
<option value="TX">TX</option>
</select><br />
<input type="submit" value="Save Location" />
</form>
<br />
<div className={className}>
{name}: {state}
</div>
</div>
);
}
ReactDOM.render(
<LocationApp />,
document.getElementById('root')
);
index.js