+ 6

Data being submitted

Okay, I tried my best but why is it not working? (I know I suck at JavaScript) https://code.sololearn.com/WEenkc5y03IJ/?ref=app

3rd Mar 2022, 11:57 PM
Junior
Junior - avatar
19 Respostas
+ 6
I've looked into the logic of your code and there are multiple logical errors. I'll list them down and show you my fixed code while explaining the fixes. If you need clarification, tag me and I'll explain further 1) You're accessing the data inside an input element using "innerHTML" attribute while you should be using "value" attribute. 2) Define the variables "name" and "pass" inside the function where you use them(inside the function "data") 3) In the if statement's condition part, you're not using parenthesis operator to specify the order they should be evaluated, so they are being evaluated in a different order than you intend them to. https://code.sololearn.com/W34q3zUz0if9/?ref=app
4th Mar 2022, 1:34 AM
Rishi
Rishi - avatar
+ 3
1) Wrap the JavaScript code inside window.onload=function(){.....} so that you won't be accessing elements before it's loaded into the browser. 2) You're setting the innerHTML of a variable that is defined in the same line. Since the assignment oprator is evaluated from right to left, it means that you're accessing the length of a variable which is not defined yet. To solve this, do the "name.innerHTML=name.length" in a different line than it's definition. Try this and let me know if it's workin
4th Mar 2022, 1:01 AM
Rishi
Rishi - avatar
+ 3
Your Mom don't edit the code that you created and attached with the question. People who see that after you corrected it, might get confused. If you edited the code that you attached with the question, save that as a new code bit using the "save as" option
5th Mar 2022, 4:45 PM
Rishi
Rishi - avatar
+ 2
Your Mom i don't understand what you're asking. What function is at the top? I removed the window.onload function because i wanted to define the variables inside the function where i use them, and the only function of that function was defining variables
4th Mar 2022, 1:54 AM
Rishi
Rishi - avatar
+ 2
data() is the only function, what do you mean by "it's at the top"
4th Mar 2022, 2:01 AM
Rishi
Rishi - avatar
+ 2
The if statement is inside the function right? How would you move the function "above" the if statement? Show me the code how you're saying and I'll get it
4th Mar 2022, 2:34 AM
Rishi
Rishi - avatar
+ 2
var password=document.getElementById("pass"); var pass=password.value.length; var namebox=document.getElementById("name"); var name=namebox.value.length; function data() { if(pass == 8 || (pass > 8 && pass <= 24) && name >= 1) { alert("Thank You! Your data has been submitted"); } }
4th Mar 2022, 2:35 AM
Junior
Junior - avatar
+ 1
Okay, you got rid of the error now but theres another error, ahen you press the login it doesnt work
4th Mar 2022, 1:05 AM
Junior
Junior - avatar
+ 1
Yea, so why do i need the function to be at the top, thats my only question
4th Mar 2022, 1:50 AM
Junior
Junior - avatar
+ 1
Function data() thats the one im talking about
4th Mar 2022, 1:54 AM
Junior
Junior - avatar
+ 1
Like when you put it any wheres else e code dont work
4th Mar 2022, 2:02 AM
Junior
Junior - avatar
+ 1
Your Mom can you show me what you mean? Where else can you put your code?
4th Mar 2022, 2:16 AM
Rishi
Rishi - avatar
+ 1
Nevermind im being an idiot
4th Mar 2022, 2:16 AM
Junior
Junior - avatar
+ 1
Your Mom it's okay even if your questions are dumb you can ask them :)
4th Mar 2022, 2:17 AM
Rishi
Rishi - avatar
+ 1
Your Mom and if i answered your question completely, mark by answer as the best so that other people will know it's answered
4th Mar 2022, 2:18 AM
Rishi
Rishi - avatar
+ 1
Yea it was just like ik there has to be a function but why is the function at the top, basically if i move the function above my if staement then the code wouldbt work
4th Mar 2022, 2:19 AM
Junior
Junior - avatar
+ 1
Like that
4th Mar 2022, 2:35 AM
Junior
Junior - avatar
+ 1
Your Mom it won't work because you're accessing the element by ID before the element is rendered by the browser. It hasn't created those elements. You're JavaScript code runs before the HTML code and so it will return a null value for the password and name. That's why I initially recommended enclosing it inside the window.onload=function... What it basically does is that it will run that code only after the window has loaded. But even after the window has loaded, the length of the name and password might be empty because the user didn't enter them yet. We can't say when the user will enter, but it's sure to be entered when the user presses the submit button. So you've to take those values when the user presses the submit button, not before the window loads or after the window loads
4th Mar 2022, 2:44 AM
Rishi
Rishi - avatar
+ 1
I think you should use length var Pw = pass.length; function submit(){ If (Pw ==8 ||(Pw > 8 && Pw <= 24) && name.length >=1){ Alert("thank you!"); }}
5th Mar 2022, 2:40 PM
Mouad ALLAOUI
Mouad ALLAOUI - avatar