-1

When a user clicks on a div tag, I want to store the corresponding id value in var current, so that it can later be used to iterate to that index, and so that div can be shown and hidden.

html

<div class="gallerypreview" id="1"></div>
<div class="gallerypreview" id="2"></div>
<div class="gallerypreview" id="3"></div>

js

var images = new Array();

$(document).ready( function(){

$('.gallerypreview').each(function() {
    images.push($(this).attr("id"));
});

$('.gallerypreview').click(function() {
var current = $('.gallerypreview').index(this);
});

});

How would I go about retrieving the div id from this clicked div? I can get the index number fine, but I cannot find the value of the index using any functions that utilizes the index #.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Dustin
  • 147
  • 1
  • 1
  • 13
  • 1
    Never begin an ID or class with a number (http://stackoverflow.com/a/79022/1418261). It can cause some browsers to not interpret the ID properly. – Jason Jun 04 '14 at 21:26
  • 1
    It looks like you are coding yourself in circles just to get a reference to a clicked div. It doesn't seem like you need the array, the iterator, even the element ids. You can get a reference to the clicked element by simply: `var current; $('.gallerypreview').click(function(){current = this;});` What are you actually trying to do? – gilly3 Jun 04 '14 at 21:41

4 Answers4

0

Is this what you are looking for?

$('.gallerypreview').click(function() {
var current = $(this).attr("id");
});

Inside the click callback function $(this) references the specific DOM object that was clicked. So a simple .attr("id") will get you the id for the clicked div (stored in the variable current).

Hope that helps

lordphnx
  • 71
  • 7
  • I need to get the id from the index only... this is bascially for an image slider, once the first div class is clicked, i need to use a moveLeft and moveRight function to find the index and use it to hide/show the next index, not the one clicked. hope that makes sense. – Dustin Jun 04 '14 at 21:31
0
var images = new Array();

$(function() {

   $('.gallerypreview').each(function() {
      images.push($(this).attr("id"));
   });

   var current = null;
   $('.gallerypreview').click(function() {
      current = $(this).attr('id');
   });
});
GGio
  • 7,563
  • 11
  • 44
  • 81
0

http://jsfiddle.net/7fPL5/

Addresses the click issue, as well as changes the IDs to valid HTML/CSS IDs (stackoverflow.com/a/79022/1418261).

JS

var images = new Array();

$(document).ready( function(){

$('.gallerypreview').each(function() {
    images.push($(this).attr("id"));
});

$('.gallerypreview').click(function() {
var current = $('.gallerypreview').index(this);
    alert(current.attr("id"));
});

});

HTML

<div class="gallerypreview" id="one">1</div>
<div class="gallerypreview" id="two">2</div>
<div class="gallerypreview" id="three">3</div>
Jason
  • 4,079
  • 4
  • 22
  • 32
0

Depending on the scope of the variable current declare it appropriately:

var images = new Array(), current;

Then change the following:

$('.gallerypreview').click(function() {
var current = $('.gallerypreview').index(this);
});

To:

$('.gallerypreview').on('click', function() {
    current = this.id;
});

JS FIDDLE DEMO

PeterKA
  • 24,158
  • 5
  • 26
  • 48