views:

49

answers:

3

I have a WP Blog with a few DIVs generated through PHP loops. I would like to be able to show additional about a DIV when someone clicks on it.

The code looks like this:

<div class="story item-1">
<div class="story item-2">
<div class="story item-3">
<div class="story item-4">

(etc. Could be any number of these.)

Clicking on one of the items above should make one of the following appear:

<div class="data preview-1">
<div class="data preview-2">
<div class="data preview-3">
<div class="data preview-4">

I'm doing the following:

$('.story').click( function() {
  var getNum   =  jQuery(this).attr('class');
  alert('hi ' + getNum);
});

But how do extract the "number" value (1,2,3,4,etc.) of the item I just clicked into a variable? If I can get that into a variable such as "num" I can then easily use jQuery .hide and .show to perform actions. What I have so far gets the entire class value. Just need the number from the second class.

I'd appreciate any help! Thanks!

A: 
$('.story').click(function() {
    var index = /item-(\d)/.exec($(this).attr('class'))[1] - 1;
    $('.data').hide().eq(index).show();
});
sje397
+2  A: 

Of course you could use string manipulation to pull out the id, but I propose a better solution:

On DOMReady, loop through all the div's, and assign an index to the elements using jQuery's lightweight data storage mechanism, .data():

Disclaimer: untested

$(document).ready(function() {
   $('div.story').each(function(index) {
      $.data(this, "num", index);
   });
});

Then you can pull it out in your click:

$('.story').click( function() {
  var getNum   =  $.data(this, "num");
  alert('hi ' + getNum);
});

That's just an example, you may end up needed extra metadata, and you can bind whatever you want to the elements, and use them later.

RPM1984
Always good to see creative uses of the `index` of `.each()` (as well as the fast access version of `$.data()`). Although, if you're going to take the data storage route, in this case I might be inclined to skip `$.data()` and take advantage of the fact that `.each()` creates a closure. If you just assign the click handlers inside the `.each()`, you can reference the persistent `index` directly. +1 :o)
patrick dw
Yes, you are right. The perfect example of the love/hate relationship we have with closures. :) Also, i just think `.data` is uber cool. :)
RPM1984
Ah, see for me it is the opposite. I'm blown away by closures, but go hot/cold on using `.data()`. Funny how that is. :o)
patrick dw
Well the reason for that is im primarily a server-side developer (so i deal with data, object scoping, etc). So when i see things like closures, i immediately gag. :)
RPM1984
+3  A: 

I'd .split() on the hyphen, and .pop() the last item (the number) off the resulting array. Then .show() the appropriate preview.

$('.story').click( function() {
    var getNum = this.className.split('-').pop();
    $('.data.preview-' + getNum).show();
});
patrick dw
Nice use of `.pop()`. +1
RPM1984
First time asking a question on Stack Overflow, didn't expect such a damn fast response! Thanks Patrick!
Sahas Katta
@Sahas - You're welcome. I think you'll find that responses here will typically be very quick, at least in the jQuery camp. :o)
patrick dw
Woops, found a bug. I'm using another selector named "selected" after the class="item-1" for instance. When I use the code you provided, it worked, but in certain cases the value getNum will include the word "selected" following the number I was looking for. How do I ignore that last item as it will be moving around to indicate a performed action?
Sahas Katta
Never mind, I figured it out! `var getNum = this.className.split('-').pop().split(' '); var getNum = getNum[0];`
Sahas Katta
@Sahas - I should have mentioned that it would be fragile to changes in the classes. Looks like you found a good solution. :o)
patrick dw