html - Javascript code getElementByID for loop -
i can't figure out why loop below doesn't work.
i've created function work html not issue. here function works:
function deactivatealltabs() { document.getelementbyid('tab-header-1').classname = 'tab-header'; document.getelementbyid('tab-header-2').classname = 'tab-header'; document.getelementbyid('tab-header-3').classname = 'tab-header'; document.getelementbyid('tab-content-1').classname = 'tab-content'; document.getelementbyid('tab-content-2').classname = 'tab-content'; document.getelementbyid('tab-content-3').classname = 'tab-content'; }
instead of hard-coding tab-headers , tab-contents ids, thought i'd create loop automatically take care of future additional tabs.
if respectively add set of:
document.getelementbyid('tab-header-4').classname = 'tab-header'; document.getelementbyid('tab-content-4').classname = 'tab-content';
everything works above. fails when try loop below. idea going wrong?
the error when clicking tab is:
uncaught typeerror: cannot set property 'classname' of null
function deactivatealltabs() { (var = 0; < tabheaders.length; i++) { document.getelementbyid('tab-header-'+i).classname = 'tab-header'; document.getelementbyid('tab-content-'+i).classname = 'tab-content'; } }
your loop starts @ 0, tab headers start @ 1.
function deactivatealltabs() { (var = 1; <= tabheaders.length; i++) { document.getelementbyid('tab-header-'+i).classname = 'tab-header'; document.getelementbyid('tab-content-'+i).classname = 'tab-content'; } }
Comments
Post a Comment