I create a button that works as a value for the input field and that input fieldwork as a value for another input field.
Because I take value in first input in a format of currency and then it passes the value to the other field without any format.
But the issue is when I click the button it works just on the first input field but does not pass the value to another field immediately until I give the value manually into the first input field.
Also when I click the button input field cannot change the value into the currency format. Kindly tell me the solution.
const num2rs = val => val.toLocaleString('en-IN', {
maximumFractionDigits: 0,
style: 'currency',
currency: 'PKR'
})
document.getElementById("userInput4").addEventListener("input", function() {
this.value = num2rs( this.value.replace(/\D/g, ""))
document.getElementById("Input.price.max").value = this.value.replace(/\D/g, "")
})
<input name="Input.price" id="Input.price.max" asp-for="Input.maxp" />
<input id="userInput4" placeholder="max" style="height :30px; width:70px; border-style:solid ;border-color: black;border-radius:5px; margin-left:20px;margin-top:5px;">
<input type="button" id="t1" value="5,00,000" onclick="document.getElementById('userInput4').value = this.value;document.getElementById('max1').value = this.value; RunGame();" style="width:70px;border-color:lightgray;background-color:white;"
/>
CodePudding user response:
I am not quite sure what you want to do, but you do not have anything in your post called max1
This one does not give errors
const num2rs = val => val.toLocaleString('en-IN', {
maximumFractionDigits: 0,
style: 'currency',
currency: 'PKR'
})
document.getElementById("userInput4").addEventListener("input", function() {
this.value = num2rs( this.value.replace(/\D/g, ""))
document.getElementById("Input.price.max").value = this.value.replace(/\D/g, "")
})
document.getElementById("t1").addEventListener("click", function() {
document.getElementById('userInput4').value = this.value;
document.getElementById('Input.price.max').value = this.value.replace(/\D/g,"");
//RunGame();
})
<input name="Input.price" id="Input.price.max" asp-for="Input.maxp" />
<input id="userInput4" placeholder="max" style="height :30px; width:70px; border-style:solid ;border-color: black;border-radius:5px; margin-left:20px;margin-top:5px;">
<input type="button" id="t1" value="5,00,000" style="width:70px;border-color:lightgray;background-color:white;" />