Is there a way to put 2 values in 1 text box using javascript? another question just dont answer if dont antw to, may i know how to if-else numbers like (enter-age 18-60 only)
<script>
function greeting() {
var name;
var name = name.value;
document.getElementById("greeting1").value = name;
var greet;
var timep = time.value;
if (timep == "M") {
greet = "Good Morning";
document.getElementById("greeting1").value = greet;
}
else if (timep == "E") {
greet = "Good Evening";
document.getElementById("greeting1").value = greet;
}
else{
alert("Not Allowed")
}
}
</script>
<body>
<p>Enter Name::</p> <input type="text" id="name" />
<p>Enter Time Period [M/E]::</p> <input type="text" id="time" />
<button onclick="greeting()">TRY</button>
<input type="text" id="greeting1" readonly />
CodePudding user response:
To append multiple values to same text field use .value = greet
function greeting() {
var name = document.getElementById("name").value;
document.getElementById("greeting1").value = name;
var greet;
var timep = time.value;
if (timep == "M") {
greet = "Good Morning";
document.getElementById("greeting1").value = greet;
} else if (timep == "E") {
greet = "Good Evening";
document.getElementById("greeting1").value = greet;
} else {
alert("Not Allowed")
}
}
<p>Enter Name::</p> <input type="text" id="name" />
<p>Enter Time Period [M/E]::</p> <input type="text" id="time" />
<button onclick="greeting()">TRY</button>
<input type="text" id="greeting1" readonly />
Solution without entering time:
function greeting() {
var name = document.getElementById("name").value;
var hour = new Date().getHours();
var greeter = document.getElementById("greeting1");
greeter.value = name;
if (hour < 12) {
greeter.value = " Good Morning";
} else {
greeter.value = " Good Evening";
}
}
<p>Enter Name::</p> <input type="text" id="name" placeholder="Name"/>
<button onclick="greeting()">TRY</button>
<input type="text" id="greeting1" readonly />
CodePudding user response:
document.getElementById("greeting1").value = `${greet} ${name}`;
// 1*String will turn string into integer, also you can do ( StringVar) or parseInt(StringVar)
if(1*document.getElementById("greeting1").value <60 ** 1*document.getElementById("greeting1").value >18) alert('Good age');