I am creating an Ember Component, and I need to bind the scroll event to a div that is dynamically created.
My handlebars code looks like this:
<div class="searchbarContainer" tabindex="0" bubbles=false>
{{input type="search" name="searchFor" class="searchTextField" placeholder="Search..." value=searchKey}}
{{#if searchKeyNotNull}}
<div class="searchResultsContainer box-shadow" {{bind-attr id="searchBarID"}}>
{{!-- BINDS ID TO searchBarID for searchResultsContainer--}}
{{#if noResults}} {{!-- Then say : "No Results. " --}}
<div class="applicantsName noResults">
No Results found.
</div>
{{else}}
{{!-- For each Row, include visual elements. --}}
{{#each toSearch }}
<div> ... </div>
{{/each}}
<div class='endOfResults'>
End of Results
</div>
{{/if}}
</div>
{{/if}}
</div>
The logic is set so that according to what is entered in the input part of this, searchKeyNotNull is updated to 'true' or 'false', as is noResults.
As it functions now, the div searchResultsContainer becomes populated with divs that contain the results, and there is a max height. I have also enabled overflow, so the user can scroll through the results.
I need to bind a scroll event to this div so that when it reaches the end, an action is fired.
Now, here's the event code I started with:
$('#searchBarID').bind('scroll',function(){
if($(this).scrollTop() + $(this).innerHeight()>=$(this)[0].scrollHeight)
{
alert('end reached');
}
});
Which works. Unfortunately, when this snippet is run, the div has not been created yet, so this event is never bound. I also tried to use the jQuery.on function, but it seems that scroll doesn't bubble, so
$('body').on('scroll','#searchBarID', function(){
if($(this).scrollTop() + $(this).innerHeight()>=$(this)[0].scrollHeight)
{
alert('end reached');
}
});
does not work.
I am at my wit's end trying to find a workaround for this; I need to either: a) find a way to bind scroll to a dynamically added element, or b) find out when Ember creates elements so I can insert this bind code right then.
A solution that does either of these two would help me greatly!