admin管理员组文章数量:1422007
I'm testing the waters with HTML/CSS/Javascript. I have a (moderately) firm grasp of the three languages separately, it's just trying to get them to cooperate with each other that is giving me issues.
My CSS and Javascript files are linked externally to my HTML file.
I'm trying to get Javascript to save user input in a variable and show the text in an alert window.
function saveUn() {
var username = document.getElementById('un').value;
alert(username);
}
<form>
Username:
<input type="text" size="12" id="un" /><br />
<input type="submit" onclick="saveUn();" />
</form>
I'm testing the waters with HTML/CSS/Javascript. I have a (moderately) firm grasp of the three languages separately, it's just trying to get them to cooperate with each other that is giving me issues.
My CSS and Javascript files are linked externally to my HTML file.
I'm trying to get Javascript to save user input in a variable and show the text in an alert window.
function saveUn() {
var username = document.getElementById('un').value;
alert(username);
}
<form>
Username:
<input type="text" size="12" id="un" /><br />
<input type="submit" onclick="saveUn();" />
</form>
Any help is greatly appreciated!
UPDATE: It turns out my problem was, in fact, that I didn't insert my external Javascript file correctly. I fixed it, and now my code works just fine. Typos ruin lives, kids.
Share Improve this question edited Nov 7, 2017 at 5:33 Paige B. asked Nov 7, 2017 at 4:47 Paige B.Paige B. 191 silver badge5 bronze badges 3- 2 Try preventDefault to prevent submitting your form. – Harsh Patel Commented Nov 7, 2017 at 4:50
- Try putting saveUn(); function on onSubmit event of the form(instead of onClick of button) with "return false" (in the function body) if you want just want to display an alert. – Abhishek Commented Nov 7, 2017 at 4:53
- I don't see the problem, this works fine in Chrome: jsfiddle/2ovdsgac – Radio Commented Nov 7, 2017 at 5:06
6 Answers
Reset to default 2try with event.preventDefault();
function saveUn(event) {
var username = document.getElementById('un').value;
alert(username);
event.preventDefault();
}
<form>
Username:
<input type="text" size="12" id="un" /><br />
<input type="submit" onclick="saveUn(event);" />
</form>
Let your callback return
false and pass that on to the onclick
handler:
<form>
Username:
<input type="text" size="12" id="un" /><br />
<input type="submit" onclick="return saveUn();" />
</form>
function saveUn() {
var username = document.getElementById('un').value;
alert(username);
return false;
}
Here is the Code that might help you.
document.getElementById("clickHere").addEventListener("click", function(event){
event.preventDefault()
var username = document.getElementById('un').value;
alert(username);
});
<form >
Username:
<input type="text" size="12" id="un" /><br />
<input type="submit" id="clickHere" />
</form>
Try this one if you do not want to submit the form and just want display an alert
:
<form onSubmit = "return saveUn();">
Username:
<input type="text" size="12" id="un" /><br />
<input type="submit" />
</form>
<script>
function saveUn() {
var username = document.getElementById('un').value;
alert(username);
return false;
}
</script>
You can return boolean value for saveUn()
, return false
if you do not want to submit
function saveUn() {
var username = document.getElementById('un').value;
alert(username);
return false;
}
<form>
Username:
<input type="text" size="12" id="un" /><br />
<input type="submit" onclick="return saveUn();" />
</form>
Using ES6 this should be better:
<form >
Username:
<input type="text" size="12" id="un" /><br />
<input type="submit" id="clickHere" />
</form>
const smBtn = document.querySelector("clickHere");
smBtn.addEventListener("click", (event) => {
event.preventDefault()
let username = document.querySelector('un').value;
alert(username);
});
本文标签: Save HTML user input to Javascript variablesStack Overflow
版权声明:本文标题:Save HTML user input to Javascript variables - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745319909a2653330.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论