Search code examples
javascriptevents

event.target.id is not a function


my code seems to work. but when I click on a button, I get the error message event.target.id is not a function

this is my javascript code

var multiplyer = 1
var carparts = 0;
document.getElementById("karpartz").innerHTML = carparts;

function onButtonClick(event) {
if (event.target.id("car"))
 {carparts = carparts + multiplyer;
  document.getElementById("karpartz").innerHTML = carparts;}} 


const button = document.querySelector('button');
button.addEventListener('click', onButtonClick);

And this is my html:

<button id="car"type="submit">
  <img src="example" 
alt="buttonpng" border="0" />
</button>

<button id="clickpower">
  upgrade click power
</button>
<button id="">
  upgrade idle scrapping
</button>

<p id="karpartz"></p>

<html>

  <body>
    <h1>
      <script type="text/javascript">
        document.write(karpartz)

      </script>
    </h1>
  </body>

</html>


what do i do?


Solution

  • apparently you have 2 issues. 1st, you are trying to use your event listener as it were a function (what it is not), therefore you are getting this error message. to resolve it you must replace event.target.id("car") to event.target.id === "car"

    2nd, you are trying to access 'karpartz' in your HTML and it is not defined in the scope of your HTML. you should use document.getElementById("karpartz").innerHTML instead document.write(karpartz)