0

I'm new to frond end development, and unfortunately have some issues calling a JavaScript function.

I have following function:

 <script type="text/javascript">
    $(document).ready(function () {

        $("#table_div").scroll(function () {
            alert("test successful");
            jQuery('#divHeader').scrollLeft(jQuery('#table_div').scrollLeft());
            jQuery('#firstcol').scrollTop(jQuery('#table_div').scrollTop());
            jQuery('#lastcol').scrollTop(jQuery('#table_div').scrollTop());
        });
    });
</script>

and HTML is defined as:

<div id="divHeader" class="firstpanel">

<div id="firstcol" class="firstcolumn">

<div id="table_div" class="contentpanel">

which is styled as:

.contentpanel {
   overflow: scroll;
   width: 300px;
   height: 500px;
   position: relative;
 }

 .firstpanel {
   overflow: hidden;
   width: 284px;
 }

 .firstcolumn {
   overflow: hidden;
   height: 500px;
 }

and I'm experiencing a problem, since the "alert" in my JavaScript function is not triggered, when I debug my application. I have made a test in JSFiddle with exacly the same code, and it works very well, since the alert function is called, once I start scrolling my div.

I'm using JQuery 1.5.1 in my application and will prefer to prevent using a newer version in JQuery. Is the JQuery version the issue in this case ?.

3
  • When are you setting the listener ? Is your html already loaded ? Commented Mar 15, 2016 at 9:33
  • Yes, I just didn't include the whole script. While I'm debugging, it hits the breakpoint in line with: $("#table_div").scroll(function () { but it doesnt trigger det srolling Commented Mar 15, 2016 at 9:35
  • Can yo try a console.log($("#table_div")); before setting the listener just to be sure ? Commented Mar 15, 2016 at 9:37

1 Answer 1

1

You are attaching the event when the element is not render by DOM. You should wrap that code in a $(document).ready() or use delegation events:

$(document).on('scroll', "#table_div", function () {
   // code 
});
Sign up to request clarification or add additional context in comments.

4 Comments

I have updated my JavaScript codeblock. This is how it actually looks.
I'm trying to convert it in a format which is supported by JQuery 1.5.1. on.('scroll') is supported from JQuery 1.7
What's the problem in upgrade the jquery version? You are using a stone age jquery library
It's not my own project, so I need to ask permission for this. Thus, I'll prevent it if it's possible

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.