/*
Assignement:
# HTML
Complete the HTML to have semantic and compliant markups.
# PURE JAVASCRIPT
Dynamically add a user to the users list.
1. Highlight the email input when a user enters an invalid email address and display following message: "please enter a valid email address" in red.
2. Use the addUser function to submit the user's data.
3. If the ajax request returns an error, display the error message in red.
4. Display the newly added user in the users list when the request was successful.
# BONUS
- make WCAG compliant
- add some CSS3 properties
*/
// START YOUR CODE HERE
document.getElementsByTagName("button")[0].addEventListener("click",function(event){
var email = document.getElementsByTagName("input")[1];
if(!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email.value))){
document.getElementById("err").innerHTML = "please enter a valid email address";
document.getElementById("err").className = "errStyle";
email.focus();
event.preventDefault();
return;
}else{
document.getElementById("err").innerHTML = "";
}
var username = document.getElementsByTagName("input")[0].value;
var email = document.getElementsByTagName("input")[1].value;
addUser(username,email,function(response){
if(response.success){
var users = document.getElementById("users");
var userLi = document.createElement("li");
userLi.innerHTML = response.user.username+"-"+response.user.email;
users.appendChild(userLi);
document.getElementById("err").innerHTML = "";
console.log(users);
}else{
var err = document.getElementById("err");
err.innerHTML = response.error;
err.className = "errStyle";
}
})
});
// END YOUR CODE HERE
// Do not modify this function. Add user service wrapper.
function addUser(username, email, callback) {
var xhr = new XMLHttpRequest();
var response;
var success = (!!Math.round(Math.random()));
if (!success){
response = JSON.stringify({
...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.