+ 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
19 Respuestas
+ 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
+ 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
+ 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
+ 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
+ 2
data() is the only function, what do you mean by "it's at the top"
+ 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
+ 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");
}
}
+ 1
Okay, you got rid of the error now but theres another error, ahen you press the login it doesnt work
+ 1
Yea, so why do i need the function to be at the top, thats my only question
+ 1
Function data() thats the one im talking about
+ 1
Like when you put it any wheres else e code dont work
+ 1
Your Mom can you show me what you mean? Where else can you put your code?
+ 1
Nevermind im being an idiot
+ 1
Your Mom it's okay even if your questions are dumb you can ask them :)
+ 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
+ 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
+ 1
Like that
+ 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
+ 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!");
}}