“Issues with variable echos” 变量回显的问题

huangapple go评论65阅读模式
英文:

Issues with variable echos

问题

My problem is that I created a little username input, and my variable in JavaScript does not assign to the HTML input box.

So my HTML is:

<!DOCTYPE html>
<html>
<head>
</head>
<body>
<label> create a username </label>
<input id="textarea"><hr>
<button id="submit">submit</button>  

<script src="index.js"></script>

</body>
</html>
let username;

document.getElementById("submit").onclick = function() {
  // I think that the problem is here, where the name gets assigned
  username = document.getElementById("textarea").value;
}
console.log(username);
英文:

My problem is that i created a little username input
And my variable in javascript does not assign to the html input box

So my html is :-

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

let username;



document.getElementById(&quot;submit&quot;).onclick = function() {

  //i think that the problem is here, where the name gets assigned

  username = document.getElementById(&quot;textarea&quot;).value;



}
console.log(username)

<!-- language: lang-html -->

&lt;!DOCTYPE html&gt;

&lt;html&gt;

&lt;head&gt;

&lt;/head&gt;
&lt;body&gt;

&lt;label&gt; create a username &lt;/label&gt;

&lt;input id = &quot;textarea&quot;&gt;&lt;hr&gt;
&lt;button id = &quot;submit&quot;&gt;submit&lt;/button&gt;  


&lt;script src=index.js&gt;&lt;/script&gt;

&lt;/body&gt;

&lt;/html&gt;

<!-- end snippet -->

I tried changing the variable to name, username, first name etc. But it didn't work

Then i tried changing the id's but that didn't work either
It always says "undefined"

答案1

得分: -1

I fixed your HTML and moved the console.log after the username has been defined, and it works as expected.

let username;

document.getElementById("submit").onclick = function() {
  // I think that the problem is here, where the name gets assigned
  username = document.getElementById("textarea").value;
  console.log(username)
}

Your original JavaScript code can be read like this:

  1. Create a new variable with no value assigned.
  2. Console.log the variable: it returns undefined.
  3. Add an event listener to the code, assign the value to the variable but do not echo it.

Instead, the fixed one does:

  1. Create a new variable with no value assigned.
  2. Add an event listener to the code, assign the value to the variable.
  3. Echo it only after it is assigned.
英文:

I fixed your html and moved the console.log after the username has been defined and it works as expected

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

let username;



document.getElementById(&quot;submit&quot;).onclick = function() {

  //i think that the problem is here, where the name gets assigned

  username = document.getElementById(&quot;textarea&quot;).value;

  console.log(username)

}

<!-- language: lang-html -->

&lt;!DOCTYPE html&gt;

&lt;html&gt;

&lt;head&gt;

  &lt;title&gt;username&lt;/title&gt;

&lt;/head&gt;
&lt;body&gt;
  &lt;label&gt; create a username &lt;/label&gt;



  &lt;input id=&quot;textarea&quot;&gt;
  &lt;hr&gt;

  &lt;button id=&quot;submit&quot;&gt;submit&lt;/button&gt;

  &lt;script src=index.js&gt;&lt;/script&gt;

&lt;/body&gt;

&lt;/html&gt;

<!-- end snippet -->

Your original js code can be read like this:

  1. create a new variable with no value assigned.
  2. console.log the variable: it returns undefined;
  3. add an event listner to the code, assign the value to the variable but do not echo it.

Instead, the fixed one does:

  1. create a new variable with no value assigned.
  2. add an event listner to the code, assign the value to the variable;
  3. echo it only after it is assigned.

huangapple
  • 本文由 发表于 2023年4月19日 17:48:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76053077.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定