0

HTML

<select style="margin-left: 10px;" id="myDropDown">
        <option value='' disabled selected>Assign Table</option>
        <option value='1'>Table1</option>
        <option value='2'>Table2</option>
        <option value='3'>Table3</option>
    </select>
<div id="myTable">
          
        </div>
        <input type="text" id="total" value="0" readonly="readonly" />

Javascript

$('#myDropDown').change(function() {
        //Selected table
        var inputValue = $(this).val();
        var HTML;
        var HTML2;
        if (inputValue == 1) {
        HTML = '<div><input id="test-element" type="checkbox" price="1">price 1</div><br><div><input id="test-element" type="checkbox" price="2">price 2 </div>';
       }
       else if(inputValue == 2) {
       HTML = '<div><input id="test-element" type="checkbox" price="3">price 3 </div><br><div><input id="test-element" type="checkbox" price="4">price 4 </div>';
      }
      document.getElementById('myTable').innerHTML = HTML;
   });
    
    function calcAndShowTotal(){
    var total = 0;
    $('#myTable :checkbox[checked]').each(function(){
        total += parseFloat($(this).attr('price')) || 0;
        alert("working");
    });
    $('#total').val(total);   
}

$(document).on("click",'#myTable :checkbox',function() {
      //alert("working");
    calcAndShowTotal();
});

calcAndShowTotal();

I made a dropdown that toggles between different checkboxes with .innerHTML. I updated the click function to be $(document).on and it works on every click but the total value doesn't change. Why doesn't calcAndShowTotal function work? I tried changing it to $(document).on too but it doesn't calculate total. Check fiddle link for example. http://jsfiddle.net/7yf8sdpw/95/

Mario
  • 13
  • 4
  • Could you add a code snippet? If you don't know how to, go here: https://stackoverflow.blog/2014/09/16/introducing-runnable-javascript-css-and-html-code-snippets/ – theknightD2 Jan 21 '21 at 01:10
  • It's not working because you're (re)creating the HTML after you've bound your events. `$("selector").click(...` only applies to elements that exist at the time it runs. You need to change to event delegation `$(document).on("click", "#myTable :checkbox", function() ...` – freedomn-m Jan 21 '21 at 01:19
  • @freedomn-m I Thanks for the input. I noticed thought that it doesn't work with $(document).on("click", "#myTable :checkbox" but rather by adding an id to the checkboxes and changing it to: $(document).on("click","#test-element". Now however it the total value function doesn't work. – Mario Jan 21 '21 at 15:12

1 Answers1

0

The div with the id of myTable does not have any children input element of type checkbox, unlike the div with id of price list.

What exactly do you want that part of the code to do?, so I can help better...

Mary Obiagba
  • 185
  • 1
  • 12
  • 1
    It does after clicking on one of the select options. It doesn't work then however because of the event delegation I assume. I'm still not sure how to change the code though. – Mario Jan 21 '21 at 14:12
  • 1
    I want this part of code that I changed to calculate total value of the checkboxes that I click. – Mario Jan 21 '21 at 16:11
  • Ok. Looks like you need to monitor the changes to #myTable in your calcAndShowTotal() function. You'll be able to get the alert to work that way. Sorry, for sending a link and not code. Codes in the comment section are not pretty at all, so here's the link to the code on code pen: https://codepen.io/ifycode/pen/OJRGZox I'm not through though, but this solution will help to begin to visualize what the problem is. – Mary Obiagba Jan 21 '21 at 17:58
  • 1
    Thanks for the reply. Since the important part was to get the total value of different "price" through clicking on checkboxes the total value from the first table option price 1 + price 2 should be = 3 and so on. Through the console I can only see one input type checkbox being logged. Also, through $(document).on the html should always be updated I believe. I tried this code by using just html and not .innerHTML and it worked so I'm still troubled, I know how to get alert to work but I'm still stuck with the total value problem, should I make a new post? – Mario Jan 21 '21 at 22:22
  • Oh ok. Sorry I didn't understand your question earlier. I thought the question implied you couldn't get #myTable to respond at all & the reason. I also didn't see that you had two input fields inside the HTML variable. Making a new post may be a good idea. Just make sure this time, you explain what you want to achieve to the reader like they're 5 years old. I'll be watching out for the new post you make, if you eventually decide to make a new one. – Mary Obiagba Jan 21 '21 at 23:33
  • Thanks for that. I played around with the code a little but there are two issues. Whenever I check a price and change table, the total number is unchanged unlike my previous code. The other problem is that even if I add another input type checkbox after for example price 1: `
    price 1
    price 2
    ` it only logs and counts the first checkbox to total.
    – Mario Jan 21 '21 at 23:46
  • Ok. I wanted to type this earlier though. All your inputs have the same id name. That could be a problem. You can only have one unique id per page. If you notice, there is no "s" in "getElementById". That is an indication that it doesn't expect to find more than one elements with same id name. I'll advice you either use class name since many elements can have same class name or use javascript to manipulate the ids so that they become unique. So this may well be related to the problem you just stated. – Mary Obiagba Jan 22 '21 at 00:14
  • I understand your point, but this function worked when I didn't use .innerHTML and put the input type checkboxes straight in in html. So I want to find out why it doesn't work with the code I posted and if it can be fixed. I'll make a new question. – Mario Jan 22 '21 at 15:01
  • Hi! I solved it after doing some more playing with the code. By calling`$(':checkbox:checked').each(function() {`every time i click. which is a rather simple solution. I don't know why it didn't work with #id checkbox[checked] though. – Mario Jan 22 '21 at 19:46