I have a script that adds a top scroll on a horizontal div. The script has to add it on up to 10 different divs. (From mainplh_boAutoOddsTable1_divScrollContainer to mainplh_boAutoOddsTable10_divScrollContainer)
To do this I call the script 10 times (below are the first 3 examples). However if one of the divs are missing it breaks the script.
doublescroll(document.getElementById('mainplh_boAutoOddsTable1_divScrollContainer'));
doublescroll(document.getElementById('mainplh_boAutoOddsTable2_divScrollContainer'));
doublescroll(document.getElementById('mainplh_boAutoOddsTable3_divScrollContainer'));
If for example mainplh_boAutoOddsTable1_divScrollContainer cannot be found it breaks my Javascript. How can I solve this? For example prevent from the function from running if it cannot find that div element?
Error message:
Uncaught TypeError: Cannot read property 'scrollWidth' of null
This is the full javascript:
function doublescroll(element) {
var scrollbar= document.createElement('div');
scrollbar.appendChild(document.createElement('div'));
scrollbar.style.overflow= 'auto';
scrollbar.style.overflowY= 'hidden';
scrollbar.style.width= '506px';
scrollbar.firstChild.style.width= element.scrollWidth+'px';
scrollbar.firstChild.style.paddingTop= '1px';
scrollbar.firstChild.appendChild(document.createTextNode('\xA0'));
scrollbar.onscroll= function() {
element.scrollLeft= scrollbar.scrollLeft;
};
element.onscroll= function() {
scrollbar.scrollLeft= element.scrollLeft;
};
element.parentNode.insertBefore(scrollbar, element);
}
doublescroll(document.getElementById('mainplh_boAutoOddsTable1_divScrollContainer'));
doublescroll(document.getElementById('mainplh_boAutoOddsTable2_divScrollContainer'));
doublescroll(document.getElementById('mainplh_boAutoOddsTable3_divScrollContainer'));
doublescroll(document.getElementById('mainplh_boAutoOddsTable4_divScrollContainer'));
doublescroll(document.getElementById('mainplh_boAutoOddsTable5_divScrollContainer'));
doublescroll(document.getElementById('mainplh_boAutoOddsTable6_divScrollContainer'));
doublescroll(document.getElementById('mainplh_boAutoOddsTable7_divScrollContainer'));
doublescroll(document.getElementById('mainplh_boAutoOddsTable8_divScrollContainer'));
doublescroll(document.getElementById('mainplh_boAutoOddsTable9_divScrollContainer'));
doublescroll(document.getElementById('mainplh_boAutoOddsTable10_divScrollContainer'));
Do a check for the existence of element
in your function:
function doublescroll(element) {
if(!element)
{
return false;
}
var scrollbar= document.createElement('div');
scrollbar.appendChild(document.createElement('div'));
scrollbar.style.overflow= 'auto';
scrollbar.style.overflowY= 'hidden';
scrollbar.style.width= '506px';
scrollbar.firstChild.style.width= element.scrollWidth+'px';
scrollbar.firstChild.style.paddingTop= '1px';
scrollbar.firstChild.appendChild(document.createTextNode('\xA0'));
scrollbar.onscroll= function() {
element.scrollLeft= scrollbar.scrollLeft;
};
element.onscroll= function() {
scrollbar.scrollLeft= element.scrollLeft;
};
element.parentNode.insertBefore(scrollbar, element);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With