0

There is a gallery which reads the data from a database.

Below of that there are some circles which shows the number of all data or records.

Each time a circle turns to yellow which indicates that what is the active post.

I have generate this mechanism by this way :

function drawCircles(activeIndex) {
    var off_html = '<img class="post_circle" src="../themes/dark/images/off.png"/>';
    var on_html = '<img class="post_circle" src="../themes/dark/images/on.png"  />';
    $('#circles').empty();

    for (var i = 1; i <= pcount; i++) {

        if (i != activeIndex) {
            $(off_html).appendTo($('#circles'));
        }
        else {
            $(on_html).appendTo($('#circles'));
        }

    }


}

PCount = Count of All Posts...

#circles div is a bar which circles are in it.**

when we call

drawCircles(2)

The second circle turns to yellow. Now I want to make a click event for that.I want to understand which circle has been clicked? I have tried .live function , but I can't found that which circle has been clicked...

1
  • note : this function will be called every 5 seconds... Commented Oct 6, 2012 at 13:59

1 Answer 1

1

Try:

$('#circles img.post_circle').on('click', function(e) {
  // e.target is the circle clicked
});

Edit: Here is a fuller answer:

$(function(){
  var off_html = '<img class="post_circle" src="http://placehold.it/50x50/ff0000"/>';
  var on_html = '<img class="post_circle" src="http://placehold.it/50x50/ffff00"/>';

  var pcount = 5;

  $('#circles').empty();    
  drawCircles(3);

  function drawCircles(activeIndex) {
    for (var i = 1; i <= pcount; i++) {
      if (i != activeIndex) {
        $(off_html).data('index', i).appendTo($('#circles'));
      } else {
        $(on_html).data('index', i).appendTo($('#circles'));
      }
    }
  }

  $('#circles img.post_circle').on('click', function(e) {
    alert($(this).data('index'));
  });
});

Here's a fiddle

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

2 Comments

you know , i want the index of circle which was clicked.
You could add a data-index attribute to each circle and read that back in the click event handler.

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.