0

I am trying to trigger a scroll event on the select tag which is being formed dynamically based on the AJAX response. Tried different approaches and nothing seems to work Tried triggering the scroll event inside a ready() in the following ways.

$(document).on("scroll", ".selectClass", function(event) {
console.log("Into document scroll");
});

$(".selectClass").scroll(function(event) {
console.log("Into scroll event");
});

$('select[id^="selectID_"]').on('scroll', function(event){
console.log("Into scroll event based on id");
});

Tried putting it in the window.load() as below

$(window).load(function(){
alert("window is loaded");
$(".selectClass").scroll(function(event) {
console.log("Into scroll event of onload");
});

Also tried putting these scroll events in script tag at the end of the body but the event is not getting triggered.

But the scroll event gets triggered when tried any of the above approaches from browser console.

Could anyone please help me.

freedomn-m
  • 27,664
  • 8
  • 35
  • 57
Srividya
  • 119
  • 1
  • 1
  • 9
  • A `select` element does not fire a scroll event. – Rory McCrossan Apr 15 '19 at 09:54
  • Ok, but i want a scroll event to be triggered on dropdown for lazy loading of elements. Another point to add, on trying these from browser console, the events are getting triggered on scroll – Srividya Apr 15 '19 at 09:55
  • In which case you would need to transpose the `select` element to a HTML structure which you can hook a scroll event to. Alternatively use a library which can already do this for you, such as Select2 – Rory McCrossan Apr 15 '19 at 09:59
  • The `document.on(` would be the only one that *would* work if your code runs before the ajax request adds the select (which is implied). Can you provide at least part of the HTML that generates the select - does it have `class=selectClass`? – freedomn-m Apr 15 '19 at 10:02
  • yes it do have the class attr with value selectClass and the drop down is formed based on the AJAX response. Tried triggering these events inside ready() as well – Srividya Apr 15 '19 at 10:16
  • 1
    There's no point trying to "trigger *these* events" **before** the select has been created (ie in `ready()`) as it won't work. See https://stackoverflow.com/questions/203198/event-binding-on-dynamically-created-elements – freedomn-m Apr 15 '19 at 10:17
  • Possible duplicate of [Event binding on dynamically created elements?](https://stackoverflow.com/questions/203198/event-binding-on-dynamically-created-elements) – freedomn-m Apr 15 '19 at 10:17
  • The class attribute through which I was trying to access the scroll event wasnt available @freedomn-m as you have mentioned, so triggered the scroll event inside another click event where I was forming the class attribute – Srividya Jun 11 '19 at 10:23

0 Answers0