skip to Main Content

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?

2

Answers


  1. The way you’re trying to access the value of the id is as if "id()" were a function. If you want to get the value of the id you can do the following.

    if (event.target.id === "car") {
      // more code 
    }
    
    Login or Signup to reply.
  2. 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)

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search