<body>
search:<input type="text" name="name" id="uniqueID" value="value" />
<button onclick="myFunction()">submit</button>
<p id="surprise"></p>
<script>
function myFunction() {
x = document.getElementById("uniqueID").value;
console.log(x);
}
</script>
</body>
If I put the input field and button inside a <form>
the value appear for second and disappear why is that? ("flashes")
By default <button>
in a form acts as <input type="submit">
so whenever you click on the button, the data is posted and the page is reloaded. The reloading makes the value disappear.
Try this :
HTML
<form method="POST/GET" action="#" onsubmit="return myFunction()">
.
.
</form>
JAVASCRIPT
function myFunction(){
x = document.getElementById("uniqueID").value;
console.log(x);
return false;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.