ga-wdi-exercises / project1

[project] GA- Project 1
3 stars 75 forks source link

how can i target what has been clicked #218

Closed mohamedrchalal closed 8 years ago

mohamedrchalal commented 8 years ago

NICK:

please take a look at what i have so far, I am not able to figure out how to select what the user has clicked on?

beckybeauchamp1 commented 8 years ago

Would you mind copying some of your code here relative to your issue? Specifically are you trying to add an event listener to an array of multiple elements? If so, once the event listener has been added, if you try to console.log(this) on the callback function of your click event, it should display the clicked element

mohamedrchalal commented 8 years ago
for(j=0;j<document.querySelectorAll('.answers').length;j++){
  document.querySelectorAll('.answers')[j].addEventListener('click',nextQuestion);
  console.log(document.querySelectorAll('.answers')[j])
  if (document.querySelectorAll('.answers').innerHTML == deadpoolTrivia.correctAnswers[i]){
    score = score +1;
  }
  }
RobertAKARobin commented 8 years ago

Start off by breaking it up into some variables. document.querySelectorAll(".answers") repeats 4 times. Do it once and save it to a variable.

mohamedrchalal commented 8 years ago

hey guys, ok i figured some of this stuff out, becky showed me yesterday what to console.log to find out what button is being clicked, and i lost that info/for got it. Could you please remind me? i know it goes in my nextQuestion function but not sure what to put in console.log('')

nolds9 commented 8 years ago

If you console.log(this) inside the callback function nextQuestion, it should display the clicked element