Abu Machi.
You are using the wrong function for the job or you have not understood exactly how it works. The function GetElementsByName, as the function names says, gets more than one element. So you need to work with a list of elements, if that's the case.
As you can see in the docs, getElementsByName is used when you need to work with more than one similar element. Otherwise, the best function for this work is getElementById. (And if you're working on modern browsers, IE8+, you can use the helpers querySelector and querySelectorAll)
Besides this issue within your code, there's one more thing to correct. When the bid value is supposedly taken, you just get the element, and not the value itself.
Anyway, let's get to what you can do. You can:
- Use getElementsByName the "correct" way.
function cal(){
var bid = Number(document.getElementsByName("bid")[0].value);
var result1 = bid * 0.1;
var result2 = bid - result1;
document.getElementsByName("fee")[0].value = result1;
document.getElementsByName("get")[0].value = result2;
}
<span style="font-size:20px;">Your bid:</span>
<input type="number" name="bid" class="form-control" style="max-width:250px; margin-top:10px;">
<br>
<input type="button" onclick="cal()">
<span style="font-size:20px;" >Our Fee (10%):</span>
<input type="number" name="fee" readonly class="form-control" style="max-width:250px; margin-top:10px;">
<br>
<span style="font-size:20px;">You'll paid:</span>
<input type="number" name="get" readonly class="form-control" style="max-width:250px; margin-top:10px;">
- Use getElementById.
function cal(){
var bid = Number(document.getElementById("bid").value);
var result1 = bid * 0.1;
var result2 = bid - result1;
document.getElementById("fee").value = result1;
document.getElementById("get").value = result2;
}
<span style="font-size:20px;">Your bid:</span>
<input type="number" id="bid" class="form-control" style="max-width:250px; margin-top:10px;">
<br>
<input type="button" onclick="cal()">
<span style="font-size:20px;" >Our Fee (10%):</span>
<input type="number" id="fee" readonly class="form-control" style="max-width:250px; margin-top:10px;">
<br>
<span style="font-size:20px;">You'll paid:</span>
<input type="number" id="get" readonly class="form-control" style="max-width:250px; margin-top:10px;">
- Use querySelector.
function cal(){
var bid = Number(document.querySelector("#bid").value);
var result1 = bid * 0.1;
var result2 = bid - result1;
document.querySelector("#fee").value = result1;
document.querySelector("#get").value = result2;
}
<span style="font-size:20px;">Your bid:</span>
<input type="number" id="bid" class="form-control" style="max-width:250px; margin-top:10px;">
<br>
<input type="button" onclick="cal()">
<span style="font-size:20px;" >Our Fee (10%):</span>
<input type="number" id="fee" readonly class="form-control" style="max-width:250px; margin-top:10px;">
<br>
<span style="font-size:20px;">You'll paid:</span>
<input type="number" id="get" readonly class="form-control" style="max-width:250px; margin-top:10px;">
And as you asked, if you want that the value change without the need to click on the button, you just need to make the of onKeyUp event on your bid input, like this:
function cal(){
var bid = Number(document.querySelector("#bid").value);
var result1 = bid * 0.1;
var result2 = bid - result1;
document.querySelector("#fee").value = result1;
document.querySelector("#get").value = result2;
}
<span style="font-size:20px;">Your bid:</span>
<input type="number" id="bid" class="form-control" style="max-width:250px; margin-top:10px;" onkeyup="cal()">
<br>
<span style="font-size:20px;" >Our Fee (10%):</span>
<input type="number" id="fee" readonly class="form-control" style="max-width:250px; margin-top:10px;">
<br>
<span style="font-size:20px;">You'll paid:</span>
<input type="number" id="get" readonly class="form-control" style="max-width:250px; margin-top:10px;">
I hope it can be any helpful.