I have a label <label onclick="functionCall()">All Models</label>
. On the click of this label i dynamically add ul to div
$("#MetricsTypeYearModelList").append('<ul class="modal__list mCustomScrollbar" id="MetricsTypeYearModelListUl"></ul>');
"MetricsTypeYearModelList" is the id of my div. After this i loop over a JSON object and add li dynamically to this ul
for (var i = 0; i < metricsTypeYearModel.length; i++)
{
var obj = metricsTypeYearModel[i];
if(i == 0)
$("#MetricsTypeYearModelListUl").append('<li data-name='+obj.ModelTypeName+' data-value='+obj.ModelTypeID+' data-id='+obj.ModelTypeID+' class="pModel active"><a href="#" id="TestClick"> '+obj.ModelTypeName+'</a></li>');
else
$("#MetricsTypeYearModelListUl").append('<li data-name='+obj.ModelTypeName+' data-value='+obj.ModelTypeID+' data-id='+obj.ModelTypeID+' class="pModel"><a href="#"> '+obj.ModelTypeName+'</a></li>');
}
After loading this, i want to click the first element of li. $( '#TestClick' ).trigger( "click" );
Problem is that when i click on the label, the div does not show up, but when i remove $( '#TestClick' ).trigger( "click" );
the div shows up. What can be the possible issue.
JS is:
$(document).on('click', 'li.pModel', function (e) {
e.preventDefault();
e.stopPropagation();
var identifier = fetchIdentifier(this);
var iHandle = $("#" + identifier); // fetch container
$(this).siblings().removeClass('active');
$(this).addClass('active');
// $('li.mModel').removeClass('active');
iHandle.find('ul li.mModel').removeClass('active');
iHandle.find('ul li.cModel').removeClass('active');
iHandle.find('ul li.mModel').fadeOut();
iHandle.find('ul li.cModel').fadeOut();
iHandle.find('ul li.mModel[data-parentid="' + $(this).data('id') + '"]').fadeToggle();
iHandle.find('ul li.mModel[data-parentid="' + $(this).data('id') + '"]:first').addClass("active");
iHandle.find('ul li.cModel').fadeOut();
iHandle.find('ul li.cModel[data-parentid="' + iHandle.find('ul li.mModel.active').data('id') + '"]').fadeToggle();
});
I want to call this JS method from the code for the first li element when i click on the label i.e. i want to click the first li element programmatically.