Click here to Skip to main content
15,890,579 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I don't want to enter the number at all input fields, just certain fields, but sometimes it need to input at all input fields and sometimes just have to input at certain input fields. If anyone know how to do it can you give the solution or how to make the code? Because when I just enter at certain input fields, it will display undefined at the input fields that doesn't have the number.

What I have tried:

This the full code:
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>


<script>
  // Listen to paste on the document
  let text = 0;
document.addEventListener("paste", function(e) {
  // if the target is a text input
  if (e.target.type === "text") {
   var data = e.clipboardData.getData('Text');
   // split clipboard text into single characters
   data = data.split(' ');
   // find all other text inputs
   [].forEach.call(document.querySelectorAll("input[type=text]"), (node, index) => {
      // And set input value to the relative character
      node.value = data[index];
    });
  }
});



</script>    <body>
<input type="text" maxlength="2" value="0">
<input type="text" maxlength="2" value="0">
<input type="text" maxlength="2" value="0">
<input type="text" maxlength="2" value="0">
<input type="text" maxlength="2" value="0">
<input type="text" maxlength="2" value="0">
    </body>
</html>
Posted
Updated 21-Jun-22 20:34pm

1 solution

Hey there, I feel like you need to clarify more on your question to get the exact solution to your problem. From my understanding, you want the default input value to be 0 and and the same time if the value was empty, you want the system to make the value to be 0.

Below is my solution to the problem.
HTML
<body>

   <input type="text" id="question_1" maxlength="2" placeholder="0">
   <input type="text" id="question_2" maxlength="2" placeholder="0">
   <input type="text" id="question_3" maxlength="2" placeholder="0">
   <input type="text" id="question_4" maxlength="2" placeholder="0">
   <input type="text" id="question_5" maxlength="2" placeholder="0">
   <input type="text" id="question_6" maxlength="2" placeholder="0">

   </body>


You can change the input type to [ type= "number" ] if you dont want anything besides number. The placeholder specifies a short hint that describes the default or specified value of an input so when someone types over it, it will simply disappear.

The next step for the empty value to be changed into value 0 is as below.
JavaScript
if(document.getElementById("question_1") || ("question_2") || ("question_3") || ("question_4") || ("question_5") || ("question_6") ).value.length == 0)
{
    alert("value will be changed from empty to 0")
}


There are many examples online but i find this easier for poeple to understand the logic behind the code.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900