-3

I making the shops for my site, and but I can't understand why this conde don't wont to work...Here's the code:

<script>
function Price() {
    var Quantity = document.getElementsByClassName('BUY')[0].value;
    var Total = Quantity * 20;
    if (!IsNan(Total)) {

        document.getElementsByClassName('Total').innerHTML = Total;
        document.getElementsByClassName('Quantity').innerHTML = Quantity;
    } else {
        document.getElementsByClassName('Total').innerHTML = "Invalid number";
        document.getElementsByClassName('Quantity').innerHTML = 0
    }
}

Here is the html:

<input style="margin-top:1%;" type="text" name="BUY" class="input" maxlength="1000" autocomplete="on" placeholder="Buy " onchange="Price()" required>Price for <span class="Quantity">0</span>Views:<span class="Total">0</span> Satoshis<br>

From this code I expect a print of the calculation based on the quantity but the code don't work and I get no errors...

Özgür Can Karagöz
  • 1,039
  • 1
  • 13
  • 32
Martino Pistis
  • 183
  • 1
  • 7

2 Answers2

-1

document.getElementsByClassName('BUY') will search for elements having class 'BUY', but ur input has class 'input'

Nizar Ahmed
  • 180
  • 9
-2

you have worng class name in the script that's why it is not working.

corrected code:(Just copy & paste it .works changed class into ID) Because using classname it is not fetching the correct value instead it assigns Quantity as undefined refer this answer

                function price(){
                    var Quantity=document.getElementById('buy').value;
                    
                    var Total=Quantity*20;
                    if(Total != 'NaN'){
    
                        document.getElementById('Total').innerHTML=Total;
                        document.getElementById('Quantity').innerHTML=Quantity;
                    }else{
                        document.getElementById('Total').innerHTML="Invalid number";
                        document.getElementById('Quantity').innerHTML=0;
                    }
                }
           
<input style="margin-top:1%;" type="text" name="BY" id="buy" maxlength="1000" autocomplete="on" placeholder="Buy " onkeyup="price()" required/>Price for 
    <span id="Quantity">0</span>
    Views:<span id="Total">0</span>
    Satoshis<br>
    
  
jasinth premkumar
  • 1,430
  • 1
  • 12
  • 22
  • 1
    hey, *man*, I didn't downvote you, but if an answer doesn't solve the problem it gets downvoted. This case is particular in that, since OP didn't do any basic debugging, you have to correct *a lot* of stuff. Good luck. – Federico klez Culloca Dec 07 '17 at 14:58
  • Also, don't complain, it isn't *manly*. If you correct your question and solve everything, chances are the downvoter will retract their downvote. EDIT: if it was not clear, I'm using "man" a lot because you did first, and I'm just teasing – Federico klez Culloca Dec 07 '17 at 14:58