Note that there are some explanatory texts on larger screens.

plurals
  1. POWrapper Function not working properly (Javascript)
    primarykey
    data
    text
    <p>I have two functions: One the validates the information in name fields of a form, and another that takes the information in those fields and prints them out in an alert box. Separately these functions work fine. I have to call them both, so I created a wrapper function. The function runs, but it refreshes instead of focusing. The weird thing is, if I check the first field, everything is fine, including the .focus();, but when I try to validate the second field, .focus(); doesn't work and the page refreshes. Any help would be appreciated. (I tried to revise my first question to add this, but when I went to save it, nothing happend.)</p> <pre><code>function main() { var test = validate(); if (test == true) { concatinate(); return true; } } function validate() { //alert ("TEST!!!"); var first = document.getElementById('firstname').value; if (first.length == 0 || first.length &gt; 25) { alert("Please enter your first name, no longer than 25 chracters."); document.getElementById('firstname').focus(); return false; } var last = document.getElementById('lastname').value; if (last.length == 0 || last.length &gt; 25) { alert("Please enter your last name, no longer than 25 characters."); document.getElementsByName('lastname').focus(); return false; } var title = document.getElementById('title').value; if (document.getElementById('title').selectedIndex == 0) { alert("Please select your salutation"); document.getElementById('title').focus(); return false; } return true; } function concatinate() { var first = document.getElementById('firstname').value; var last = document.getElementById('lastname').value; var title = document.getElementById('title').value; var fullname = title + " " + first + " " + last; var printFull = "Welcome, " + fullname; alert(printFull); } &lt;form name="name" form id="name" method="post" onsubmit="return main();"&gt; Salutation: &lt;select name="title" select id="title"&gt; &lt;option selected="Please Select"&gt;Please select&lt;/option&gt; &lt;option value="Mr."&gt;Mr.&lt;/option&gt; &lt;option value="Mrs."&gt;Mrs.&lt;/option&gt; &lt;option value="Miss"&gt;Miss&lt;/option&gt; &lt;/select&gt;&lt;br&gt;&lt;br&gt; First Name : &lt;input type="text" input id="firstname" name="firstname"&gt; Last Name : &lt;input type="text" input id="lastname" name="lastname"&gt;&lt;br&gt;&lt;br&gt; &lt;input type="submit" value="Submit"&gt;&lt;br&gt;&lt;br&gt; &lt;/form&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload