admin管理员组文章数量:1429918
Am new to javascript i foud this one in w3schools - Entering pincode in search box will show in <p>
tag !! How to do ? any help appreciated. Thanks in advance
HTML :
<form class="form1" role="search" method="get" action="search-pincode.html">
<input class="pincode1" type="number" placeholder="Enter Postcode" id="user-pincode">
<button class="submit" type="submit" formaction="search-pincode.html" onclick="myFunction()">Check</button>
</form>
JS SCRIPT :
<script>
function myFunction() {
var x = document.getElementById("user-pincode").value;
document.getElementById("user-pincode-show").innerHTML = x;
}
</script>
OUTPUT : search-pincode.html
<h1 class="search-h1-contact">
Yes, we install CCTV in <p id="user-pincode-show"></p>
</h1>
Am new to javascript i foud this one in w3schools. - Entering pincode in search box will show in <p>
tag !! How to do ? any help appreciated. Thanks in advance
HTML :
<form class="form1" role="search" method="get" action="search-pincode.html">
<input class="pincode1" type="number" placeholder="Enter Postcode" id="user-pincode">
<button class="submit" type="submit" formaction="search-pincode.html" onclick="myFunction()">Check</button>
</form>
JS SCRIPT :
<script>
function myFunction() {
var x = document.getElementById("user-pincode").value;
document.getElementById("user-pincode-show").innerHTML = x;
}
</script>
OUTPUT : search-pincode.html
<h1 class="search-h1-contact">
Yes, we install CCTV in <p id="user-pincode-show"></p>
</h1>
Share
Improve this question
edited Nov 16, 2017 at 9:54
Hameed Basha
asked Nov 16, 2017 at 9:48
Hameed BashaHameed Basha
1491 silver badge12 bronze badges
0
3 Answers
Reset to default 3For one, you will not see the result long enough because after clicking the button, you are being redirected to search-pincode.html
. Try changing type="submit"
to type="button"
function myFunction() {
var x = document.getElementById("user-pincode").value;
document.getElementById("user-pincode-show").innerHTML = x;
}
<form class="form1" role="search" method="get" action="search-pincode.html">
<input type="number" placeholder="Enter Postcode" id="user-pincode">
<button type="button" onclick="myFunction()">Check</button>
</form>
<h1 class="search-h1-contact">
Yes, we install CCTV in
<p id="user-pincode-show"></p>
</h1>
If what you are planning is to pass the value of the input to search-pincode.html
, and user-pincode-show
is inside it, then using JavaScript is not proper way to do it
If you want to avoid redirecting on form submition you can declare an event listener using EventTarget.addEventListener() to handle the click event and call e.preventDefault
in the function handler..
Also notice that there is no need to add formaction="search-pincode.html"
in the button
element because the form
has the attribute action="search-pincode.html"
And last thing: With in the h1
element you can better use a span
element instead of p
.
<h1>Yes, we install CCTV in <span id="user-pincode-show"></span></h1>
Code example:
document
.querySelector('button.submit')
.addEventListener('click', function (e) {
e.preventDefault();
var x = document.querySelector('#user-pincode');
document.querySelector('#user-pincode-show').innerHTML = x.value;
});
<form class="form1" role="search" method="get" action="search-pincode.html">
<input class="pincode1" type="number" placeholder="Enter Postcode" id="user-pincode">
<button class="submit" type="submit">Check</button>
</form>
<h1>Yes, we install CCTV in <span id="user-pincode-show"></span></h1>
function myFunction() {
var x = document.getElementById("user-pincode");
document.getElementById('user-pincode-show').innerHTML = x.value;
}
<form class="form1" role="search" method="get" action="search-pincode.html">
<input type="number" placeholder="Enter Postcode" id="user-pincode">
<button type="button" onclick="myFunction()">Check</button>
</form>
<h1 class="search-h1-contact">
Yes, we install CCTV in
<p id="user-pincode-show"></p></h1>
本文标签: javascriptHow to get the search box value in ltpgt tagHTMLjsStack Overflow
版权声明:本文标题:javascript - How to get the search box value in <p> tag - HTML,JS - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745001851a2637025.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论