mardi 28 juin 2016

checkbox null exception javascript


function goMovies() 
{
    alert("Getting Movies...");
    $.getJSON('NikkiNacksProducts.json', function(nikkitable) 
    {
        var output="<table>";
        for (var i in nikkitable.products) 
        {

            if (nikkitable.products[i].productType == "Movie")
            {
            output+= "<tr><td><input id = '"+i.toString() +"c' type='checkbox'></td><td>" + nikkitable.products[i].productId + "</td><td>" + nikkitable.products[i].productName + "</td><td>" + nikkitable.products[i].productType + "</td><td>" + nikkitable.products[i].newRelease + "</td><td>" + nikkitable.products[i].productPrice+ "<td><input id = '"+i.toString() +"t' type='text' name='Number of copies' > </td></tr>";
            }
        }
         output+="</table> <button onclick="total();">";
        document.getElementById("placeholder").innerHTML=output;
  });}
</script>

<script>
function total()
{
   var totala = 0.0;
$.getJSON('NikkiNacksProducts.json', function(nikkitable)
 {
        for (var i in nikkitable.products)
  {
         var temp ="'"+i.toString() +"c'"
         if(document.getElementById(temp)!="null")
         {
         if(document.getElementById(temp).checked)
         {
           temp ="""+i.toString() +"t""
           totala += parseFloat(document.getElementById(temp).value) * nikkitable.products[i].productPrice;
         }
}
  }     
  });
alert(totala);
}
</script>

The goMovies() creates a checkbox which is accessed by the total(). It's returning => typeError: document.getElementById(...) is null. nikkitable is the json file containing an of arrays of movies and songs


Aucun commentaire:

Enregistrer un commentaire