tags:

views:

183

answers:

2

I am running this loop in php but using a jquery click function during the output. How can i record which instance of $i was clicked. When I use $(this) it returns the id.

The $('#pid-form').val('$frame_id[$i]') actually has the number i want as the value but I am not sure how to use it or convert it to a variable that I can use.

for($i = 0; $i < $numRecords2; $i++){

$prod_id = $_SESSION['prod_array'][$frame_id[$i]];
echo"

$('#frame$frame_id[$i]').click(function() {
 $('#top-left-frame').html('<img src=\"$upper_left[$i]\" alt=\"\" />');
 $('#top-mid-frame').css('background-image','url($upper_middle[$i])'); 
 $('#top-right-frame').html('<img src=\"$upper_right[$i]\" alt=\"\" />');
 $('#mid-left-frame').css('background-image','url($middle_left[$i])'); 
 $('#mid-right-frame').css('background-image','url($middle_right[$i])'); 
 $('#bottom-left-frame').html('<img src=\"$bottom_left[$i]\" alt=\"\" />');
 $('#bottom-mid-frame').css('background-image','url($bottom_middle[$i])'); 
 $('#bottom-right-frame').html('<img src=\"$bottom_right[$i]\" alt=\"\" />');
 $('#frame-select').html('$prod_id');
 $('#pid-form').val('$frame_id[$i]');
 $('#oa_id-form').val('$prod_id');
 var frameState = $(this).attr('id');
 $.cookie('frameState', frameState, { expires: 7 });  
    alert($.cookie('frameState') + ' was clicked.');
 });
";
 }
A: 

Why not simply hard-code it as you've done everywhere else in your script already?

NOTE: This is a very bad design IMHO; instead of creating all of this for EACH element in your array, you should create a static JS function that does all of this, and then call it with parameters to pass along the extra information.

ken
I did not code it. I am trying to work with it.
Kauthon
A: 

if "frame$frame_id[$i]" is the clicked object's id, you could possibly extract the id with:

$(this).attr('id').replace(/[^\[]*\[([^\]]*)\]$/, '$1')

this would manipulate the id and return only the value between the brackets.

n0nick
didn't work exactly as planned but led me down the path to the right answer. I just did a replace on the 'frame' with '' and got the number. Thanks a ton.
Kauthon