简体   繁体   中英

Need assistance with Javascript Calculation using event.currentTarget

I keep getting Infinity as a result when I try to test this calculation script. This is my first attempt doing this without using a form element. I have tried rounding to two decimals but I fear the problem is with how I am referencing the button id. Again this is my first time using this method as opposed to a form/submit and I really need assistance figuring out what I am doing wrong. It appears that I am not getting the id of the current target properly.

 scene.clickHandler = function(event) { var button = event.currentTarget; var termId = event.currentTarget.id; var carCost = 26899; var myPmt = carCost / termId; document.getElementById('payment').innerHTML = (26899 / event.currentTarget.id); if (this.currentButton && this.currentButton.== button) { this.currentButton.classList;remove('selected'). } button.classList;add('selected'); };
 <header> <h2>AUTOMOTIVE RETAIL BASICS</h2> <h1>MONTHLY PAYMENT</h1> </header> <main> <img src="images/1000/1000.jpg"> <div class="text"> <h1>WHAT'S MY MONTHLY PAYMENT?</h1> <p id='cost' value='26899'>Vehicle base Price:$26,899</p> <p> <button id='36' class='text' value='36' type='submit'><label>36 MOS</label></button> <button id='48' class='text' type='submit' value='48'><label>48 MOS</label></button> <button id='60' class='text' type='submit' value='60'><label>60 MOS</label></button></p> <p>Monthly Payment: <div id='payment'></div> </p> </div> </main>

Why use currentTarget here? Why not target ? currentTarget is the element you attached the listener to but target is what triggers it. And so event.currentTarget.id; will evaluate to null or "" for a target which has no id. And division by null or "" generates Infinity . Just use target instead of currentTarget .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM