-1

So im new to javascript and i got this code from the internet thats supposed to on the click of the arrows increase or decrease "magic points" by taking from or adding to the "points left: " but its not working. i keep getting the error: Uncaught ReferenceError: incAtt is not defined at HTMLButtonElement.onclick i know that decAtt function is not written yet but i cant seem to get the incAtt function to work, i tried document.getElementById("incAtt").addEventListener("click", function() then defining id="incAtt" but that didnt work either any ideas why its not working? its working perfectly on the website i copied this from.

</table>
 <span onclick="incAtt('magic');" style="cursor: pointer;">&#x25B2;</span>
 <span onclick="decAtt('magic');" style="cursor: pointer;">&#x25BC;</span>

            <div id="magic" style="display:inline;">0</div>

          </td>
         </tr>
         <tr>
            <td style="text-align:right;">
              Points left:
            </td>
            <td>
              <div id="points" style="display:inline;">10</div>
            </td>
<table>
           <script function incAtt(att)> {
                pts = parseInt($('#points').html());
                flag = (pts > 0);
                if (flag) {
                  n = parseInt($('#' + att).html());
                  $('#' + att).html(n + 1);
                  $('#points').html(pts - 1);
                }
            }
            </script>
Warchant
  • 3
  • 2

1 Answers1

0

It seems your code has a problem. Take a look at this part of code <script function incAtt(att)> { this's not valid syntax especially for calling the method inside the javascript.

Try this code instead. This will fix your problem.

<script> 
    function incAtt(att){
           pts = parseInt($('#points').html());
           flag = (pts > 0);
           if (flag) {
               n = parseInt($('#' + att).html());
               $('#' + att).html(n + 1);
               $('#points').html(pts - 1);
           }
    }
</script>
Jesper Martinez
  • 611
  • 5
  • 15
  • yes apparently the syntax wasn't correct i noticed that now, however even with the correct syntax its not working and i get a new error Uncaught ReferenceError: $ is not defined at incAtt at HTMLButtonElement.onclick – Warchant Apr 15 '20 at 04:50
  • If that was your next issue. Make sure you have include "jQuery" on your main directory. – Jesper Martinez Apr 15 '20 at 04:57
  • Thank you so much! this fixed my problem. – Warchant Apr 15 '20 at 05:00