admin管理员组文章数量:1327665
I have a button, when I click on it a form is shown, It has and input field name, I am trying to display input field value in a another div after submitting the form. But I am unable to do it. What I am doing wrong?.
<!DOCTYPE html>
<html>
<head>
<script>
function showDiv() {
document.getElementById('hello').style.display = "block";
}
function showValue(){
var name = document.getElementById('name').value;
document.getElementById('ans').innerHTML = name;
}
</script>
</head>
<body>
<input type="button" onclick="showDiv()" value="click on me"/>
<form id="hello" style="display:none;">
<label>Enter Name: </label><br>
<input type="text" id="name" required>
<input type="submit" value="submit" onclick="showValue()">
</form>
<div id="ans"></div>
</body>
</html>
Please help me, thanks In advance.
I have a button, when I click on it a form is shown, It has and input field name, I am trying to display input field value in a another div after submitting the form. But I am unable to do it. What I am doing wrong?.
<!DOCTYPE html>
<html>
<head>
<script>
function showDiv() {
document.getElementById('hello').style.display = "block";
}
function showValue(){
var name = document.getElementById('name').value;
document.getElementById('ans').innerHTML = name;
}
</script>
</head>
<body>
<input type="button" onclick="showDiv()" value="click on me"/>
<form id="hello" style="display:none;">
<label>Enter Name: </label><br>
<input type="text" id="name" required>
<input type="submit" value="submit" onclick="showValue()">
</form>
<div id="ans"></div>
</body>
</html>
Please help me, thanks In advance.
Share Improve this question asked Aug 1, 2018 at 8:17 Jamie SouthwellJamie Southwell 932 gold badges2 silver badges8 bronze badges2 Answers
Reset to default 3The value actually appears in the div
but your form submits immediately. One way to get away is to not use submit button. Change input[type=submit]
to button[type=button]
.
This will prevent the form from being submitted.
function showDiv() {
document.getElementById('hello').style.display = "block";
}
function showValue() {
var name = document.getElementById('name').value;
document.getElementById('ans').innerHTML = name;
document.getElementById('hello').style.display = "none";
}
<input type="button" onclick="showDiv()" value="click on me" />
<form id="hello" style="display:none;">
<label>Enter Name: </label><br>
<input type="text" id="name" required>
<button type="button" onclick="showValue()">submit</button>
</form>
<div id="ans"></div>
function showDiv() {
document.getElementById('hello').style.display = "block";
}
function showValue() {
var name = document.getElementById('name').value;
document.getElementById('ans').innerHTML = name;
document.getElementById('hello').style.display = "none";
}
<input type="button" onclick="showDiv()" value="click on me" />
<form id="hello" style="display:none;">
<label>Enter Name: </label><br>
<input type="text" id="name" required>
<button type="button" onclick="showValue()">submit</button>
</form>
<div id="ans"></div>
本文标签: htmlshow input field value in div using javascriptStack Overflow
版权声明:本文标题:html - show input field value in div using javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742227876a2436664.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论