What's wrong with my code? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
25th Sep 2020, 10:04 PM
KWASI NYARKO
KWASI NYARKO - avatar
31 Answers
+ 15
Just do it this way <form onSubmit="checkAddress()"> Email: <input type="email" id="email"/> <input type="submit" value="Submit"/> </form> <script> let em=document.getElementById("email"); function checkAddress(){ if(em.value === ""){ alert("Email address is required"); } } </script> </body> </html> sololearn places script tag in head ,that might be causing the problem Also i never tried passing global id as a parameter to a function ,maybe you can't just do it that way
25th Sep 2020, 10:41 PM
Abhay
Abhay - avatar
+ 5
checkaddress vs checkadress.. thats the issue...
26th Sep 2020, 8:48 AM
John Rodrigo
John Rodrigo - avatar
+ 3
There is a "." After getElementById ,remove it and also there is no id like "fieldid " Edit: sorry didn't noticed there is email id which is passed to function Odyel ty
25th Sep 2020, 10:07 PM
Abhay
Abhay - avatar
+ 3
This works as well ,you can pass id as a parameter ,the only problem was not passing it as a string but as a variable lol! <body> <form onSubmit="checkAddress('email')"> Email: <input type="email" id="email"/> <input type="submit" value="Submit"/> </form> <script> function checkAddress(email){ if ( document.getElementById(email).value === ""){ alert("Email address is required"); } } </script> </body>
25th Sep 2020, 11:03 PM
Abhay
Abhay - avatar
27th Sep 2020, 12:21 PM
S.G.INGALE
S.G.INGALE - avatar
+ 2
Abhay I don't know a lot about web dev, this is out of curiosity, isn't fieldId just the variable name that's holding the passed value "email"? Or is that just code etiquette
25th Sep 2020, 10:12 PM
Odyel
Odyel - avatar
+ 2
KWASI NYARKO your JS for checkAddress is missing a d
25th Sep 2020, 10:13 PM
Odyel
Odyel - avatar
+ 2
KWASI NYARKO One of the main reasons for your code not working was that your HTML form onSubmit event trigger called checkAddress(), but in the JS the function was mis-spelled checkAdress() - only one 'd'. Then fix for script running before HTML elements are loaded. I prefer to keep my JS in its own tab, rather than on the HTML which is why I'm showing you this solution. JS: let em; function checkAddress(){ if (em.value === ""){ alert("Email address is required"); } } window.onload = function() {em = document.getElementById("email")};
26th Sep 2020, 8:46 AM
Russ
Russ - avatar
+ 1
KWASI NYARKO html, change the email's type to email not text
25th Sep 2020, 10:16 PM
Odyel
Odyel - avatar
+ 1
Odyel thanks but alert message still doesn't show. 😒
25th Sep 2020, 10:27 PM
KWASI NYARKO
KWASI NYARKO - avatar
25th Sep 2020, 10:29 PM
Odyel
Odyel - avatar
+ 1
I'm trying to write a code that will display a message when there are no texts and the submit button is clicked
25th Sep 2020, 10:32 PM
KWASI NYARKO
KWASI NYARKO - avatar
+ 1
Man I wish I knew anything about JS, I don't know. Abhay could you give is a shot? Dunno a thing about web
25th Sep 2020, 10:39 PM
Odyel
Odyel - avatar
+ 1
I'm not on here for follows, just answering questions. Don't think you can learn much from me; most of the stuff I know is OOP programming. And I only go on here to check recent questions, if you really want me to then sure. But it'll be superficial
25th Sep 2020, 10:43 PM
Odyel
Odyel - avatar
+ 1
Thanks Abhay . It worked!! 💥🦸
25th Sep 2020, 10:46 PM
KWASI NYARKO
KWASI NYARKO - avatar
+ 1
Thank you all
26th Sep 2020, 2:56 PM
KWASI NYARKO
KWASI NYARKO - avatar
27th Sep 2020, 3:26 AM
U Hariharan
U Hariharan - avatar
+ 1
You have a name a function wrong In html it is check address And in jscripr it is adress
27th Sep 2020, 1:11 PM
Tayyib
Tayyib - avatar
+ 1
Why not just use the simple HTML attribute "required" That's the commonest method Check this 👇 out https://code.sololearn.com/WgAQKj5K2w1v/?ref=app And the spelling of your "checkAddress" was also wrong in the JS code
27th Sep 2020, 7:03 PM
Eriline
Eriline - avatar
0
Done. Thanks error cleared but the alert message doesn't show
25th Sep 2020, 10:10 PM
KWASI NYARKO
KWASI NYARKO - avatar