Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I prevent a js function from running when a searched after getElementById cannot be found?

Tags:

javascript

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'));
like image 577
Kaah Avatar asked Dec 26 '22 00:12

Kaah


1 Answers

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);
    }
like image 188
scrappedcola Avatar answered May 04 '23 11:05

scrappedcola