I have a .JS include file, that applies to several pages. However, for one page I have now adopted a jquery method, and to use
$("").on("click","#id",function(){});
on some of my elements. But now when I goto a page that doesnt have that element on it, It flags up as an error. Is there anyway around this so that It just simply ignores these elements if they dont appear on the page? Otherwise I would have to write seperate .JS files for each page and I’m sure that’s not the correct way to do this.
Thanks for your help. 🙂
(note: i am specifying tags etc i was just giving an example, i apolagise, i removed the tag name. Lets call the tag #mydiv. When using the above clause, on a page that doesnt have #mydiv, then i get a console error. It is perfectly fine on the pages that DO have #mydiv, however! So the question is: how can i stop that error from being caught?)
Your error should be related with something else since
onwon’t throw any error if some element doesn’t exist. Try withIf could also use an inner container instead of
body