Eliminating warning in HTML
Question by user1221330
I am creating 3 jsp pages. There are 4 boxes with different id in each pages (i.e. box1, box2, box3, box 4 in page 1; box 5, box6, box7, box8 in page 2; box9, box10, box11, box12 in page 3). Below is the sample code in page 1:
<div class="dragableBox" id="box1">CAT</div>
<div class="dragableBox" id="box2">DOG</div>
<div class="dragableBox" id="box3">HORSE</div>
<div class="dragableBox" id="box4">TIGER</div>
In each page there is also a script. In the script I deliberately use all those ids above as parameters of a function. Below is the sample code in page 1:
dragDropObj.addSource('box1',true);
dragDropObj.addSource('box2',true);
dragDropObj.addSource('box3',true);
dragDropObj.addSource('box4',true);
dragDropObj.addSource('box5',true);
dragDropObj.addSource('box6',true);
dragDropObj.addSource('box7',true);
dragDropObj.addSource('box8',true);
dragDropObj.addSource('box9',true);
dragDropObj.addSource('box10',true);
dragDropObj.addSource('box11',true);
dragDropObj.addSource('box12',true);
I must do this because as far as I know this is the only way for my program to work. The problem I encounter is that each time the program started, a warning appears:
“The source element with id box5 does not exist”
Although the program still works fine with this warning, I still want to eliminate the warning.
My question here is:
How can I stop such warning from appearing?
Is there a kind of error catching method in HTML?
Answer by Starx
Check the existence of element before adding them.
if (typeof document.getElementById('box5') !== 'undefined'){
// continue
}