I'm working on This Problem on the first studio of module 8 in CS50x. Basically I just want edit that label and apply the CSS with Javascript if an error is encountered, but I don't know enough about the syntax to do so. I've looked everywhere on the web and nothing has helped me break through. Any advice? I've included snippets of code that might paint you a better picture of what I need to do.
// first we get a bunch of DOM elements that we will need:
// the form
var form = document.getElementById("form-google");
// the div container that holds stuff relating to the username field
var usernameContainer = document.getElementById("field-username");
// the text input where the user shold type their name
var usernameInput = usernameContainer.querySelector("input"); // the querySelector() function gives us the first <input> child element of usernameContainer
// the div container that holds stuff relating to the search ("q") field
var searchContainer = document.getElementById('field-search'); // TODO something similar to usernameContainer
// the text input where the user types their search query
var searchInput = searchContainer.querySelector('input'); // TODO something similar to usernameInput
// set a function that will be executed every time the form is submitted
form.onsubmit = validate(event);
clearFeedback(searchContainer);
var search = searchInput.value;
var searchIsValid = search.length > 0;
if (searchIsValid == false) {
giveFeedback(searchContainer, "Search something!");
}
/**
* given a container <div> and an error message, sets the child <label> to display the error message,
* and sets the child <input> to have the "input-invalid" class so that our CSS will give it a red border
*/
function giveFeedback(container, message) {
// TODO
// HINT: do something similar to clearFeedback() (see below)
}
/**
* given a container <div>, resets the <input> and <label> inside to be "normal", ie not colored red or reporting any errors
*/
function clearFeedback(container) {
var textInput = container.querySelector("input");
textInput.className = "";
var feedbackLabel = container.querySelector(".feedback-error");
feedbackLabel.innerHTML = "";
}
<form id="form-google" action="https://www.google.com/search" method="get">
<div id="field-username" class="container-field">
<label>Your name:</label>
<br/>
<input name="username" type="text"/>
<label class="feedback-error">EDIT THIS LABEL</label>
</div>
<div id="field-search" class="container-field">
<label>Search:</label>
<br/>
<input name="q" type="text"/>
<label class="feedback-error">OR THIS LABEL</label>
</div>
<br/>
<input type="submit" value="Googly Moogly Search"/>
</form>
// CSS
.input-invalid {
border-color: red;
border-width: 1px;
border-style: solid;
}
.feedback-error {
color: red;
}