Matt Swezey
Matt Swezey

Reputation: 379

JQuery Selecting an element within a for each loop

So the last line of code selects all the "a" elements on the page that are within "transactiontable". I need it to only select the "a" element that lies with the current "tr" element as each URL for each "tr" element is unique.

Currently all the URLs have the same id value as from the last iteration of the for each function.

Any help would be greatly appreciated. Thanks

Ignore "[[ @{'~' + ${flowExecutionUrl}(_eventId='existingVehicle')} ]]" as that is Thymeleaf.

$(document).ready(function() {  
            $('#items-list tr').each(function() {
                var id = $(this).attr('id').split('-')[1];
                var url3 = "\u0026id=" + id;

                var url = [[ @{'~' + ${flowExecutionUrl}(_eventId='existingVehicle')} ]] + url3;

                $('.transactiontable a').attr('href', url);
            });
});

Upvotes: 0

Views: 73

Answers (1)

Michael Anthopolous
Michael Anthopolous

Reputation: 231

This should do it:

$(document).ready(function() {  
    $('#items-list tr').each(function() {
        var id = $(this).attr('id').split('-')[1];
        var url3 = "\u0026id=" + id;

        var url = [[ @{'~' + ${flowExecutionUrl}(_eventId='existingVehicle')} ]] + url3;

        $(this).find('.transactiontable a').attr('href', url);
    });
});

The '$(this)' selector represents the current item within the scope of the iterator, and 'find' then performs the selection within that element.

Upvotes: 2

Related Questions