I saw this post and tried to copy it but it didn’t work –
Syncing scrolling
I have a TabContainer with 2 tabs that have divs, bmrDetailDataDiv and residentDetailDataDiv.
Here is my javscript code –
window.onload = function () {
var bmrDetailDiv = $("div[id$='_bmrDetailDataDiv']")[0];
var residentDetailDiv = $("div[id$='_residentDetailDataDiv']")[0];
if ((bmrDetailDiv) && (residentDetailDiv)) {
bmrDetailDiv.on('scroll', function () {
residentDetailDiv.scrollTop(bmrDetailDiv.scrollTop());
});
residentDetailDiv.on('scroll', function () {
bmrDetailDiv.scrollTop(residentDetailDiv.scrollTop());
});
}
Am I missing something?
EDIT –
Tried this and get an error –
$(document).ready(function () {
var bmrDetailDiv = $("div[id$='_bmrDetailDataDiv']");
var residentDetailDiv = $("div[id$='_residentDetailDataDiv']");
if (bmrDetailDiv.length && residentDetailDiv.length) {
bmrDetailDiv.on('scroll', function () {
residentDetailDiv.scrollTop($(this).scrollTop());
});
residentDetailDiv.on('scroll', function () {
bmrDetailDiv.scrollTop($(this).scrollTop());
});
}
});
EDIT #2 –
Tried this and still getting JScript error. This is all within my .js file that get’s included at the top of the page. –
jQuery.fn.exists = function () { return this.length > 0; }
$(function () {
var combined = $("div[id$='_bmrDetailDataDiv']").add($("div[id$='_residentDetailDataDiv']"));
if (combined.exists()) {
//Getting error on below line
combined.on("scroll", function () {
combined.scrollTop($(this).scrollTop());
});
}
});
Always check the console – that will cause errors because you are attempting to use jQuery methods on native elements (since you retrieved them via
[0]). If you were doing this purely for the sake of theifcondition, there’s no need – to check the selectors found elements, you can just query thelengthproperty.Other changes:
1) Document ready handler instead of
window.onload2) Use of
$(this)inside event callback