Xenyal
Xenyal

Reputation: 2223

Appending id within jQuery function call?

Is there a method to append #dimensionName_ inside handleResults() with current_index of add_filter_Form?

I have the following code:

this.add_filter_form = function (name, value, status) {
                    form_count++;
                    current_index++;
                    form_status[current_index] = status;

                    $("#_IWID_Filters_included").append("<div id='filter_" + current_index + "'>" +
                        "Filter name: <select id = 'dimensionName_" + current_index + "'></select>" +
                        "Filter value: <select id ='dimensionId_" + current_index + "'></select>" +
                        "<button type='button' id='remove_btn_" + current_index + "' onclick='filter_forms.remove_filter_form(" + current_index + ")' style='margin-top: 5px; margin-bottom: 5px;'>Remove filter</button>" +
                        "</div>");
                }

function handleResults(responseObj) 
            {
                //Populates every dimensionName_[current_index] with names:
                $("#dimensionName_").html(responseObj.DimensionListItem.map(function(item) 
                {
                    return $('<option>').text(item.dimensionDisplayName)[0];
                }));
            }

Im looking for something along the effects of:

$("#'dimensionName_ + current_index'")

Upvotes: 1

Views: 88

Answers (2)

kasper Taeymans
kasper Taeymans

Reputation: 7026

you need to pass the current index variable to the handleResults function. you also need to call that function in the scope of the current_index variable or save a copy in the objects scope from within an other function. so you could use this.current_index

function handleResults(responseObj, current_index) {...

you need to concatenate the strings. just use + sign for that.

$("#dimensionName_"+current_index)...

Upvotes: 1

Alex
Alex

Reputation: 10216

From what I've understood you are setting the current_index via add_filter and need this value asynchronously inside handleResults. Therefor, you need to make this variable globally available:

var current_index = 0;

this.add_filter_form = ...

function handleResults(responseObj){
  $("#dimensionName_" + current_index).html();
}

Upvotes: 1

Related Questions