-1

I have problem with my javascript. I'm not able to display the results in the input field. Please can anyone help me?? And if it possible it will be better to calculate the result without clicking on the button!! Thanks.

<script>
    function cal(){
        
        var bid = document.getElementsByName("bid");
        var result1= bid * 0.1;
        var result2= bid - result1;
        document.getElementsByName("fee").value = result1;
        document.getElementsByName("get").value = result2;

    }
    </script>
<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;">
Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
Abu Machi
  • 121
  • 2
  • 13

5 Answers5

2

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:

  1. 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;">
  1. 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;">
  1. 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.

1

Only form-field elements have a value attribute/property. For regular elements, you want either the .textContent (for plain text) or the .innerHTML (for strings with HTML in them) properties.

Next, avoid inline styles as they just make the HTML more difficult to read and lead to duplication of code. On the same vein, don't use inline HTML event attributes (onclick). There are many reasons for that.

Next, don't use .getElementsByName() as this returns a "live node list", which is only right for a few use cases and can be wasteful otherwise. Give your form fields, not only a name attribute, but also a (usually) matching id attribute as well.

Next, don't use form elements if you aren't expecting users to interact with them (the fee and total areas).

See comments in code below:

// Get references to the DOM elements you will need just once:
var fee = document.getElementById("fee");
var get = document.getElementById("get");
var bid = document.getElementById("bid")
var btn = document.querySelector("input[type=button]");

// Set up event handling in JavaScript, not HTML:
btn.addEventListener("click", cal);

// You can also have the cal function run as the user enters 
// their bid. No button click needed.
bid.addEventListener("input", cal);

function cal(){
  var result1 = bid.value * 0.1;
  var result2 = bid.value - result1;
  fee.textContent = result1;
  get.textContent = result2;
}
span {
  font-size:20px;
}

.formField {
   max-width:250px; 
   margin-top:10px;
}
<span>Your bid:</span>
<input type="number" id="bid" name="bid" class="form-control" class="formField"><br>
<input type="button">
<span>Our Fee (10%):</span>
<span id="fee"></span><br>
<span>You'll paid:</span>
<span id="get"></span>
Scott Marcus
  • 64,069
  • 6
  • 49
  • 71
  • You got wrong result on "YOU WILL PAID: " , and anyway I got the right answer thanks!!! – Abu Machi Nov 16 '17 at 21:33
  • @AbuMachi I corrected that. But, read through my answer. Just because you got it to work doesn't mean you are doing it correctly. You aren't. – Scott Marcus Nov 16 '17 at 21:34
  • @AbuMachi I added a second event handler so that the calculation is done automatically as you enter your bid. No need to click the button. – Scott Marcus Nov 16 '17 at 21:35
  • Why it is not correct if I just instead of getElementsByName use getElementsById...? and get the value from the user and store at the first variable (bid) ? – Abu Machi Nov 16 '17 at 21:36
  • @AbuMachi As I stated in my answer, `.getElementsByName()` returns a "live" collection of elements and you aren't looking for a collection, you are looking for a single element. `.getElementById()` does that and is the most efficient way to do it. There is no `.getElementsById()` (plural). – Scott Marcus Nov 16 '17 at 21:38
  • @AbuMachi Read through my entire answer. There are several things you are doing that should be corrected. Also, I've added code so that you don't have to click the button. – Scott Marcus Nov 16 '17 at 21:39
  • I understood the difference between ByName and ById – Abu Machi Nov 16 '17 at 21:40
  • @AbuMachi There are several other problems with your code. – Scott Marcus Nov 16 '17 at 21:40
  • I'm beginner so it is normally that someone who has experience is better than me, but I'm trying to learn as much as possible and as correctly as possible of course – Abu Machi Nov 16 '17 at 21:42
  • @AbuMachi I understand. That's why I urge you to read through my entire answer for all the details on how you should change your approach. – Scott Marcus Nov 16 '17 at 21:43
  • @ScottMarcus There is no idea to explain these kind of things to clearly very junior people, who don't want or are not ready to hear, in SO. When they grow, they'll learn and understand importance of good structure by themselves or just stay on level they are and lose interest in programming eventually :) – Rauli Rajande Nov 16 '17 at 21:43
  • @RauliRajande I've been a professional IT trainer for over 20 years. Your comment is just plain wrong. New learners should most definitely be exposed to proper coding techniques from the start. There is nothing that is complex about my answer and I've provided details for each recommendation. – Scott Marcus Nov 16 '17 at 21:44
  • I read what you wrote to me, and I know what you want to tell me, if you thinks that I don't know the structure of coding you are not right sure, but it this point it's not important for me, I just want to learn how to write the answer in the input field. I'm new web developer but not new to programming! – Abu Machi Nov 16 '17 at 21:46
  • @AbuMachi I'm not trying to teach you programming. I'm trying to teach you proper web development. You can read my answer and learn from it or not. But, as I said, just because it works, doesn't mean you did it right. – Scott Marcus Nov 16 '17 at 21:48
  • @ScottMarcus I agree with you. Only problems are, that a) they might not be ready for such a complexity, b) they are really not interested in right way or c) they don't have time right now for right way. Additionally, there might be many correct ways :) – Rauli Rajande Nov 16 '17 at 21:49
  • Okay, sorry for I previous answer, I will give my best to learn from you (your code) and anyone who is "better" than me. Thanks because you want to explain me the proper my coding in js. Because really I start teaching js before only several days. – Abu Machi Nov 16 '17 at 21:50
  • 1
    @RauliRajande, as I said I'm not to programming so believe me it's not complex for me, and second I'm very interested in right way to develop, but really I don't have time to make it to that way because I have deadline, and I have to do more. But no one said that I will not come back to learn that "right" way of implementing this. – Abu Machi Nov 16 '17 at 21:55
  • @Rauli a) there’s nothing complex here. b) not my problem if they aren’t interested in the right way. c) not having time to do it right === lazy and, while there are usually more than one correct ways, the OP’s code doesn’t use any of them. Remember SO isn’t just about answering the question, it’s a knowledge base for others who will come along later. Answers should strive to be complete and accurate and not include bad practices. – Scott Marcus Nov 16 '17 at 22:01
  • @AbuMachi I have learned in my time in IT, that ALWAYS the primary thing is that system has to work. Good practises is secondary effect of this, these help make and keep things to work. But there is no use of "correctly built" system that doesn't do its thing. There comes my warning - do not blindly adopt any "correct way", but first try to understand its purpose and if this is applicable on your case. And if "correct way" overloads your thinking, so that you are not able to make the system work, then this is not the way you should do this right now. – Rauli Rajande Nov 16 '17 at 22:06
  • @Rauli That is terrible advice that is well proven to lead to brittle solutions that break easily. A house that isn’t built on a solid foundation may “work” for a limited time, but will eventually collapse. – Scott Marcus Nov 16 '17 at 22:12
  • @ScottMarcus I believe in refactoring and introducing separation of concerns gradually. When there is such an hasty script, it can stay there for some time, but when this needs functional improvement or gets connected to other parts of the system or there is just some spare development resource, then it has to be refactored to something nicer. This is such a big topic :) I have seen a lot of things collapsing and even never getting finished because of overabstraction. – Rauli Rajande Nov 16 '17 at 22:27
0

You should use id instead of name to get elements in javascript and you saved the input field in the variable bid instead of saving the value of the input field.

<html>

<head>
  <script>
    function cal() {

      var bid = document.getElementById("bid").value;

      var result1 = bid * 0.1;
      var result2 = bid - result1;

      document.getElementById("fee").value = result1;
      document.getElementById("get").value = result2;



    }
  </script>
</head>

<body>
  <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;">

</body>

</html>
niklassc
  • 597
  • 1
  • 9
  • 24
0

try it :

 function cal(){

            var bid = document.querySelector("input[name=bid]");
            var result1= bid * 0.1;
            var result2= bid - result1;
            document.querySelector("input[name=fee]").value = result1;
            document.querySelector("input[name=get]").value = result2;

        }

or

function cal(){

        var bid = 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;

    }

Or you can set id to all inputs and use documment.getElementById For use function after change you can use next code:

var bid = document.getElementsByName("bid")[0];
bid.addEventListener('change',function(){
  cal();
});
Petrashka Siarhei
  • 733
  • 10
  • 12
0

getElementsByName gives you an array of elements.

Use zeroeth element for value:

<script>
    function cal(){

        var bid = 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;

    }
</script>

Edit - without clicking you can update your inputs when you add to your input this:

onkeyup="cal()"
Rauli Rajande
  • 2,010
  • 1
  • 20
  • 24
  • I got the answer, thanks....but if you know how to do this calculation without clicking on the button it will be very helpful for me! – Abu Machi Nov 16 '17 at 21:21