User Tools

Site Tools


en:web_development:forms:javascript

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
en:web_development:forms:javascript [2022/01/26 17:04]
mag
en:web_development:forms:javascript [2023/08/16 09:33] (current)
Line 734: Line 734:
 <code>    var status = document.getElementById('status'); <code>    var status = document.getElementById('status');
     if (message == "") {     if (message == "") {
-        document.getElementsByName('add_hero').submit();+        document.getElementsByName('add_hero')[0].submit();
     } else {     } else {
         status.firstElementChild.innerHTML = message;         status.firstElementChild.innerHTML = message;
Line 796: Line 796:
   * For this, we can use a 'while statement'.   * For this, we can use a 'while statement'.
   * A 'while statement' says to do something while a condition is true.   * A 'while statement' says to do something while a condition is true.
-  * In this case, we need to check that the checkbox actually exists (<html>n < movieList.length</html> will do this) AND that the movie isn't checked, which we can test with <html>!movieList[n].checked).+  * In this case, we need to check that the checkbox actually exists (<html>n < movieList.length</html> will do this) AND that the movie isn't checked, which we can test with <html>!movieList[n].checked</html>).
   * Our loop is then as follows.   * Our loop is then as follows.
 <code>    while((n < movieList.length) && (!movieList[n].checked)) { <code>    while((n < movieList.length) && (!movieList[n].checked)) {
Line 816: Line 816:
         message += "You need to add at least one movie. ";         message += "You need to add at least one movie. ";
     }</code>     }</code>
 +  * There's one last small problem we need to address with this form.
 +  * If the 'new' checkbox is checked, we need to ensure that there is a movie title in the adjacent text box.
 +  * How do we check the status of 'new' checkbox? It's the second last 'input' (the last is the one for the movie name) so the index is 2 less than the length of 'movieList'.
 +<code>movieList[movieList.length-2].checked</code>
 +  * The value of the movie name input is <html>movieList[movieList.length-1].value</html> and we only need a message if both the checkbox is checked AND the name field is blank. So our code needs to be as follows.
 +<code>    if ((movieList[movieList.length-2].checked) && (movieList[movieList.length-1].value == "")) {
 +        message += "If you check the last box, you need to name the movie.";
 +    }</code>
 +  * Add this to your 'addAppearance' function.
   * Finally, we can determine whether to submit the form or show a message in the same way as for the previous form.   * Finally, we can determine whether to submit the form or show a message in the same way as for the previous form.
 <code>    var status = document.getElementById('status'); <code>    var status = document.getElementById('status');
     if (message == "") {     if (message == "") {
-        document.getElementsByName('add_hero').submit();+        console.log('ready to submit');
     } else {     } else {
         status.firstElementChild.innerHTML = message;         status.firstElementChild.innerHTML = message;
         status.style.display = 'block';         status.style.display = 'block';
     }</code>     }</code>
 +  * Save, upload and test this code.
 +
 +==== Sharing Code ====
 +  * Before we enable the submission, take a look at the whole 'verifyAddAppearance' function and notice that aside from the actual submit command, it all applies to 'verifyDeleteAppearance' too.
 +  * If we rewrite the entire code, then find we need to change something (for example, we update the form), we need to change this code twice too. Far better to share the same code for both functions.
 +  * To do this, we create a new function. Let's call it 'verifyAppearance' and move all the shared code into the new function.
 +<code>function verifyAppearance() {
 +    var message  = "";
 +    var noAppearance = 0;
 +    var alias = document.getElementsByName('alias')[1].value;
 +    if (alias == "") {
 +        message += "The Hero is required. ";
 +    }
 +    var movieList = document.getElementById('movieList').getElementsByTagName('input');
 +    var n = 0;
 +    while((n < movieList.length) && (!movieList[n].checked)) {
 +        console.log(n);
 +        noAppearance++;
 +        n++;
 +    }
 +    console.log(noAppearance);
 +    if (noAppearance == movieList.length) {
 +        message += "You need to add at least one movie. ";
 +    }
 +    if ((movieList[movieList.length-2].checked) && (movieList[movieList.length-1].value == "")) {
 +        message += "If you check the last box, you need to name the movie.";
 +    }
 +    console.log(message);
 +}</code>
 +  * We then call 'verifyAppearance' from our 'verifyAddAppearance' function.
 +<code>function verifyAddAppearance() {
 +    // check form data
 +    verifyAppearance();
 +
 +    // take action - display message or submit
 +    var status = document.getElementById('status');
 +    if (message == "") {
 +        console.log('ready to submit');
 +    } else {
 +        status.firstElementChild.innerHTML = message;
 +        status.style.display = 'block';
 +    }
 +}</code>
 +  * The problem now is that the main function doesn't know the value of 'message' so it can't decide whether to submit or show the message.
 +  * We need to 'return' message to the main function. Add this code to the end of verifyAppearance.
 +<code>    return(message);</code>
 +  * Then we can assign that value to 'message' in verifyAddAppearance.
 +<code>    var message = verifyAppearance();</code>
 +  * Save, upload and test. Is it working correctly?
 +
 +==== Uncheck Checkbox ====
 +  * We still have to write the code for 'verifyDeleteAppearance'.
 +  * It's almost exactly the same as for 'verifyAddAppearance' except for the submit command which we haven't written yet, so let's start by copying the code.
 +<code>function verifyDeleteAppearance() {
 +    // check form data
 +    var message = verifyAppearance();
 +
 +    // take action - display message or submit
 +    var status = document.getElementById('status');
 +    if (message == "") {
 +        console.log('ready to submit');
 +    } else {
 +        status.firstElementChild.innerHTML = message;
 +        status.style.display = 'block';
 +    }
 +}</code>
 +  * There is one other difference. We don't want to delete 'new' because it doesn't exist.
 +  * Rather than checking whether it's checked asking the user to uncheck it, let's provide a better user experience by unchecking it automatically.
 +  * Try this in your console first.
 +<code>movieList[movieList.length-2].checked = false;</code>
 +  * That should set the checkbox to an unchecked state.
 +  * We want to do the same thing in our function, but it doesn't know about 'movieList' yet.
 +  * We can't easily return that from 'verifyAppearance' because we are already returning a value, but we can send it as a parameter when we call the function (or we could just define it twice).
 +<code>    // check form data
 +    var movieList = document.getElementById('movieList').getElementsByTagName('input');
 +    var message = verifyAppearance(movieList);</code>
 +  * Now 'verifyAppearance' can obtain this variable as a parameter in the top line.
 +<code>function verifyAppearance(movieList) {</code>
 +  * And we can delete the line where we assign it in that function.
 +  * Now, we can uncheck the checkbox in 'verifyDeleteAppearance'.
 +<code>    // uncheck 'new' checkbox
 +    movieList[movieList.length-2].checked = false;</code>
 +  * Save, upload and test that everything works as it should.
 +  * If it does, the final step is to add the commands to submit each form.
 +<code>        document.getElementsByName('add_appearance')[0].submit();</code>
 +<code>        document.getElementsByName('add_appearance')[0].submit();</code>
 +  * Save, upload and test again.
 +  * Congratulations. You have now created a functional form.
 +
 +[[en:web_development:forms:exercises|Next: Exercises]]
en/web_development/forms/javascript.1643245440.txt.gz · Last modified: 2023/08/16 09:33 (external edit)