-1

I want to convert an input id string in a integer. I am testing this js functions, despite getPrice is working, getQty gives me "undifine" instead of a number. What is wrong?

function getPrice() {
var price = document.getElementById("priceTshirt").innerHTML;
document.getElementById("total-1").innerHTML = price;
}

getPrice();


function getQty() {
var qty = document.getElementById("qty1").innerHTML;
document.getElementById("demolish").innerHTML = qty;
qty = qty.parseInt;
}

getQty();
PMA
  • 91
  • 2
  • 13
  • 1
    `qty = parseInt(qty)` or better yet `qty = +qty`. `parseInt` is a global *function*, not a property of a string... – Andrew Li Feb 20 '17 at 02:18
  • 1
    Keep in mind the differences between `parseInt()` and `new Number()`: https://stackoverflow.com/questions/4090518/what-is-the-difference-between-parseint-and-number – Daniel T. Feb 20 '17 at 02:20
  • @AndrewLi that makes sense , I forgot to add it to itself, otherwise I will be changing it value to nothing at all, or in this case undefined. I will try it now and let you know. – PMA Feb 20 '17 at 02:22
  • @AndrewLi I tried but I did not work. – PMA Feb 20 '17 at 02:29

1 Answers1

1

Qty1 is an input element, it doesn't have innerHTML. You should get the value instead:

function getQty() {
    var qty = document.getElementById("qty1").value;
    document.getElementById("demolish").innerHTML = qty;

    return qty;
}

and you can set the total price using the quantity returned by the getQty function:

function getPrice() {
    var price = parseInt(document.getElementById("priceTshirt").innerHTML);
    document.getElementById("total-1").innerHTML = price * getQty();
}
Andrés Andrade
  • 2,213
  • 2
  • 18
  • 23