javascript - How can i check if my DOM already contains an element by id? -


i know if possible check if dom contains element id. dynamically load templates (kendo templates) , append them in body.

<body>     ...      <script type="text/x-kendo-template" id="test-view">         ...     </script> </body> 

at moment loading script

//creates gloabl object called templateloader single method "loadexttemplate" var templateloader = (function ($, host) {     //loads external templates path , injects in page dom     return {         //method: loadexttemplate         //params: (string) path: relative path file contains template definition(s)         loadexttemplate: function (path) {             //use jquery ajax fetch template file             var tmplloader = $.get(path)                 .success(function (result) {                     //on success, add templates dom (assumes file has template definitions)                     var regsplit = new regexp("[/\]+", "g");                     var pathtab = path.split(regsplit);                     var templatename = pathtab[pathtab.length - 1];                     var regreplace = new regexp("[.]+", "g");                     var templatename = templatename.replace(regreplace, "-");                     var s = $("<script type=\"text/x-kendo-template\" id=\"" + templatename + "\">" + result + "</script>");                     $("body").append(s);                 })                 .error(function (result) {                     alert("error loading templates -- todo: better error handling");                 })              tmplloader.complete(function () {                 //publish event indicates when template done loading                 $(host).trigger("template_loaded", [path]);             });         }     }; })(jquery, document); 

before loading template need check if 1 loaded. need check if script id="test-view" exists or not. how can that?

var element = $('#' + templatename) if(!element.length) {    // create template } 

Comments

Popular posts from this blog

java - Jmockit String final length method mocking Issue -

asp.net - Razor Page Hosted on IIS 6 Fails Every Morning -

c++ - wxwidget compiling on windows command prompt -