+ 6
What's wrong with my code?
32 Respostas
+ 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
+ 5
checkaddress vs checkadress.. thats the issue...
+ 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
+ 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>
+ 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
+ 2
KWASI NYARKO your JS for checkAddress is missing a d
+ 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")};
+ 1
KWASI NYARKO html, change the email's type to email not text
+ 1
Odyel thanks but alert message still doesn't show. š
+ 1
Not sure what else to say, works just fine for me
https://www.sololearn.com/post/613843/?ref=app
https://code.sololearn.com/We5U5YZCN0Oy/?ref=app
+ 1
I'm trying to write a code that will display a message when there are no texts and the submit button is clicked
+ 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
+ 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
+ 1
Thanks Abhay . It worked!! š„š¦ø
+ 1
Thank you all
+ 1
https://code.sololearn.com/WgdfkOZJxwmC/?ref=app
KWASI NYARKO here is the solution ....
+ 1
You have a name a function wrong
In html it is check address
And in jscripr it is adress
+ 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
0
Done. Thanks error cleared but the alert message doesn't show