I have a page containing few div tags as follows
<div id="dynamic_widget_466" class="dynamic_widget_466"></div>
<div id="dynamic_widget_468" class="dynamic_widget_468"></div>
<div id="dynamic_widget_464" class="dynamic_widget_464"></div>
<div id="dynamic_widget_462" class="dynamic_widget_462"></div>
I am trying to get all the values of these div tags into an array variable using jquery and add content to these div tags. The script is as follows
$(document).ready(function (){
    var dynaWidgets = [];
    dynaWidgets = document.querySelectorAll('div[id^="dynamic_widget_"]');
    for(var i=0; i<dynaWidgets.length; i++)
    {
        var dynaWidgetId = dynaWidgets[i].id;
        var widgetId = dynaWidgetId.substring(3).split('_')[2];
        console.log('The selected contentId  : ' + dynaWidgetId );
        $.ajax({  
            url: "${CONTEXTPATH}/widgets/dynamic-widget",
            type: "GET",
            data: {    
                contentId: widgetId
            },
            // the type of data we expect back
            dataType : "html",
            success: function( htmlData ) 
            {
                $('#' + dynaWidgetId).html(htmlData);
                console.log('The selected dynaWidgetId ' +i+ ' : ' + dynaWidgetId );
            },
            error: function( xhr, status, errorThrown ) {
                console.log( "Error: " + errorThrown );
                console.log( "Status: " + status );
                console.dir( xhr );
            },
            complete: function( xhr, status ) {
            },
        });
     }
});
Even though I am able to get the values in these divs, the content gets added only to the last div. Please help me out. Thanks in advance
 
    