Jquery loop - grabbing value

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

$ ('# pid-form'). val ('$ frame_id [$ i]') actually has a number that I want as a value, but I'm 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.');
 });
";
 }

      

+1


a source to share


2 answers


if "frame $ frame_id [$ i]" is the id of the clicked object, you can retrieve it with:

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

      



this will manage the id and only return the value between the brackets.

0


a source


Why not just hard-code it like you've already done everywhere in your script?



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

0


a source







All Articles