0

So, I have 10 balls with the same class and different ids from one to ten.

<section class="stage" >
    <figure class="circle ball" id="1"><span class="shadow"></span></figure>
        </section>
        <section class="stage" >
            <figure class="circle ball" id="2"><span class="shadow"></span> 
    </figure>
        </section>
        <section class="stage" >
    <figure class=" circle ball" id="3"><span class="shadow"></span></figure>
</section>

...

How can I get on click id that is a number and that is stored in variable? I tried querySelector but it only gets me first match, then querySelectorAll but it returns nodelist... I am fairly new to js so if someone have some solution...

Thanks in advance

FatBoyGebajzla
  • 159
  • 1
  • 2
  • 10

6 Answers6

1

Use querySelectorAll('.circle.ball') to get all those elements that belong to that class and then use addEventListener() to assign click event. After that you can get the id value by this.id when you click an element.

var elem = document.querySelectorAll('.circle.ball');
for(var i=0; i<elem.length; i++){
  elem[i].addEventListener('click',clickCircle);
}

function clickCircle(){
  console.log(this.id)
}
<section class="stage" >
    <figure class="circle ball" id="1"><span class="shadow">1</span></figure>
        </section>
        <section class="stage" >
            <figure class="circle ball" id="2"><span class="shadow">2</span> 
    </figure>
        </section>
        <section class="stage" >
    <figure class=" circle ball" id="3"><span class="shadow">3</span></figure>
</section>
Ankit Agarwal
  • 30,378
  • 5
  • 37
  • 62
0

Run the code snippet and click on the green boxes ...

var balls = document.querySelectorAll('.ball');   // get all balls

balls.forEach(function(ball){  // loop over all balls to use every ball
    ball.addEventListener('click', function(){  // every ball needs to listen on "click" event
        console.log('The ball id is: ' + this.id); // when clicked log the id.
    });
});
.ball {
    background-color:green;
    margin: 10px;
    width: 40px;
    height: 40px;
    cursor: pointer;
}
<section class="stage" >
    <figure class="circle ball" id="1"><span class="shadow"></span></figure>
        </section>
        <section class="stage" >
            <figure class="circle ball" id="2"><span class="shadow"></span> 
    </figure>
        </section>
        <section class="stage" >
    <figure class=" circle ball" id="3"><span class="shadow"></span></figure>
</section>
caramba
  • 21,963
  • 19
  • 86
  • 127
0

document.querySelectorAll('.ball').forEach(link => {
    link.addEventListener('click', function(event) {
         console.log(event.currentTarget.id);
    });
});
<section class="stage" >
    <figure class="circle ball" id="1"><span class="shadow"></span></figure> 
</section>
<section class="stage" >
    <figure class="circle ball" id="2"><span class="shadow"></span></figure>
</section>
<section class="stage" >
    <figure class=" circle ball" id="3"><span class="shadow"></span></figure>
</section>
Omar Muscatello
  • 1,256
  • 14
  • 25
0

Hi you can try this.

var x = document.getElementsByClassName("circle ball");
var i;
var yourId;
for (i = 0; i < x.length; i++) {
    yourId = x[i].id;
}
prady
  • 416
  • 2
  • 6
  • 17
0

Your problem is that .querySelector only returns one element.

To target multiple elements, you have to use .querySelectorAll.
Then you can use .forEach to add a listener on all your elements:

document.querySelectorAll('.ball').forEach(function(ball) {
  ball.addEventListener('click', function() {
    console.clear();
    console.log('You clicked id:', this.id);
  });
});
.ball {
  width: 0;
  height: 0;
  border: 20px solid gray;
  border-radius: 50%;
}
<section class="stage">
  <figure class="circle ball" id="1"><span class="shadow"></span></figure>
</section>
<section class="stage">
  <figure class="circle ball" id="2"><span class="shadow"></span>
  </figure>
</section>
<section class="stage">
  <figure class=" circle ball" id="3"><span class="shadow"></span></figure>
</section>

Hope it helps.

Takit Isy
  • 9,688
  • 3
  • 23
  • 47
-1

Try this in your html:

onclick="myFunction(this)"

Then this in your js file:

myFunction(item){
  console.log(item)
}
Jerry
  • 1,455
  • 1
  • 18
  • 39