JavaScript How to get the search box value in <p> tag - HTML,JS

Am new to javascript i foud this one in w3schools.com - 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>
Answer:1

For 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>
Answer:2

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>
Answer:3

I'm trying to figure out how to compare if certain elements in two arrays compare in the same order. var compare = function (arr1, arr2) { //........ } compare ([f,t,r,m], [s,f,t,r,q,p,m]); /...

I'm trying to figure out how to compare if certain elements in two arrays compare in the same order. var compare = function (arr1, arr2) { //........ } compare ([f,t,r,m], [s,f,t,r,q,p,m]); /...

Read me: --> Im having troubles when I hover the flip card, if you move the cursor while hovering inside the div is triggering continuously and it makes an annoying effect, so here is what I need: 1) ...

Read me: --> Im having troubles when I hover the flip card, if you move the cursor while hovering inside the div is triggering continuously and it makes an annoying effect, so here is what I need: 1) ...

  1. issue with visual voicemail

I have a function that is inside a factory API.doRetrieveResults = function(name, id, values, row) { // alert(angular.toJson(values)); var getdata = { 'operation': 'RetrieveResults', '...

I have a function that is inside a factory API.doRetrieveResults = function(name, id, values, row) { // alert(angular.toJson(values)); var getdata = { 'operation': 'RetrieveResults', '...

I want to code to calculate the total number of adult, child and baby passengers. I tried with jquery but did not count the total number of all the passengers. Thanks in advance https://jsfiddle.net/...

I want to code to calculate the total number of adult, child and baby passengers. I tried with jquery but did not count the total number of all the passengers. Thanks in advance https://jsfiddle.net/...

  1. calculate total number of combinations
  2. calculate total number of days
  3. calculate total number of allowances
  4. calculate total number of hours
  5. calculate total number of possible combinations
  6. calculate total number of permutations
  7. calculate total number of hours in excel
  8. calculate total number of days in excel
  9. calculate total number from percentage
  10. calculate total number of outcomes
  11. calculate total number of atoms
  12. calculate total number of rows in excel
  13. calculate total number of subnets
  14. calculate total number of microstates
  15. calculate total number of options
  16. calculate total number of ions in a compound
  17. calculate total number of cells in excel
  18. calculate total number of coalitions
  19. calculate total number in excel
  20. calculate total number of stereoisomers