1

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.

Waleed Naveed
  • 2,293
  • 2
  • 29
  • 57

1 Answers1

0

You can use $("#TestClick").click() to explicitly call the click event.

Use .on to create bindings on dynamically created elements.

function functionCall()
{

  $("#MetricsTypeYearModelList").append('<ul class="modal__list mCustomScrollbar" id="MetricsTypeYearModelListUl"></ul>');

  var metricsTypeYearModel =  ["List Item 1","List Item 2","List Item 3"];

  for (var i = 0; i < metricsTypeYearModel.length; i++)
  {
    var val = metricsTypeYearModel[i];
    var id = i==0 ? 'id="TestClick"' : '';
    var li = '<li data-name='+val+' data-value='+val+' data-id='+val+' class="pModel active"><a href="#" '+ id +'> '+val+'</a></li>'
    
    $("#MetricsTypeYearModelListUl").append(li);
  }

  $("#TestClick").on('click', function(){

    console.log($(this).text() + " invoked the click");

  });

  $("#TestClick").click();

}
#MetricsTypeYearModelList
{
 border: solid 1px red;
}
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
</head>
<body>
<label onclick="functionCall()">All Models & Click Here</label>

<div id="MetricsTypeYearModelList">
This is DIV content and it shows up
</div>
</body>
</html>
Hary
  • 5,690
  • 7
  • 42
  • 79