0

I have managed to create divs (I use css to make them blue circles) dynamically and add unique id attributes dynamically too, I'd like to also add unique onclick events also but I am struggling to manage it. Here is my JavaScript code followed by the HTML I'm working with:

var i = 0;

function addCircles(click) {
  var numberOfCircles = document.getElementById('setSize').value;
  var startFromOne = numberOfCircles + 1 - 81;
  document.getElementById('debug').innerHTML = numberOfCircles;

  for (i = 1; i < startFromOne; i++) {
    var element = document.createElement('div');
    element.id = "circle" + i;
    element.className = "circle";
 // element.onclick = attack(key);
    document.getElementById('gamecanvas').appendChild(element);
    document.getElementById("circle" + i).innerHTML = i;
  }
}

function attack(key) {
  if (key === 1) {
    document.getElementById('circle1').style.backgroundColor = purple;
  }
/* This is a tester, if circle1 turns purple onclick I'm good to go. */
}

I want to be able to click the divs (blue circles) and change their colour.

4
  • Where are you getting the variable key from? Also if you write attack(key), it will execute the function, rather than binding it to an event handler. Commented Jan 29, 2015 at 22:24
  • You can use getElementsByClassName('circle') which creates a NodeList that you can loop through and attach a click event to. Commented Jan 29, 2015 at 22:25
  • Try to use "function attack(event)" and then in "event.target" you should have the element that has been clicked and in consecuence in "event.target.id" the id of the element. Commented Jan 29, 2015 at 22:29
  • I want to give each div I make onclick="attack(n)" where n will be increments of 1, so the first div with id="circle1" will have onclick="attack(1)" and then id="circle2" will have onclick="attack(2)" and so on. :) Forgive me I'm very new to this! Commented Jan 30, 2015 at 0:01

2 Answers 2

1

This will assign the doAttack() function to all your elements with the class circle. Then we just change the color of the circle inside the function!

var circles = document.getElementsByClassName('circle');

for ( var x in circles ) {
    circles[x].onclick = doAttack;   
}

function doAttack() {
    this.style.background = 'purple';
}

Edit

Here is a Demo

If you wish to do an if statement inside your function just compare the this.id to whatever you want. if ( this.id === 'circle1' ) { .. }

Sign up to request clarification or add additional context in comments.

Comments

0

Another solution is to add an onclick event to the main div:

<div onclick="doAttack(event)">
    <div class="circle" id="circle1">
    </div>
    <div class="circle" id="circle2">
    </div>
    <div class="circle" id="circle3">
    </div>
    <div class="circle" id="circle4">
    </div>
</div>

And then in your doAttack function:

function doAttack(event) {
    document.getElementById(event.target.id).style.backgroundColor = 'red';
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.