jquery - Applying Javascript classes to generated content before it is applied to DOM -


i have situation whereas generating content (video links) via ajax call in javascript object. once content returned backend, html string generated , appended current html in dom. html string contains video links wrapper in 'div'. need apply new javascript object each of these new generated divs , have done using following code:

if(response.videos.length != 0) {            jquery(response.videos).each(function(key, video) {                          self.html += "<div class='col-md-4' data-related-video='" + video.videoid + "'>";                 self.html += '<a href="#" class="dashboard-video"></a>';                 self.html += '<div class="video-information">';                     self.html += '<h5 class="tk-bebas-neue">' + video.videoname + '</h5>';                     self.html += '<p>' + video.videodesc + '</p>';                     self.html += '<p>equipment: ';                      self.html += "</p>";                     ......................................more code                      self.html += "</div>";                 self.html += '</div>';             self.html += "</div>";             newratingarraykey.push("#" + self.relateddiv + " div[data-related-video=" + video.videoid + "] .video-rating");         });              jquery("#" + jquery(self.element).attr("data-related") + " .col-md-8").append(self.html);                    jquery(newratingarraykey).each(function(key, element) {             element = jquery(element);                           newratingobject[key] = new rater(element, jquery(element).attr("data-rel"), jquery(element).find(".star"), jquery(element).attr("data-user-id"));         });      } 

as can see, after have generated new html string, add dom loop through new divs apply 'rater' class them. question is, there way apply 'rater' class each new generated div before gets applied dom? makes me think page may open error if fails between new content being added , rater class being applied new content. thanks!

you don't have satisfy concerns. browser won't repaint page until code finishes executing, , neither other javascript code execute potentially access dom before add 'rater' classes.

if wanted outlined, use unattached element(e.g $('<div/>')) add content to, before transplanting onto active dom.

but best solution not concatenate strings create dom elements. rather, use documentfragment , createelement api, freely add classes choose.


Comments

Popular posts from this blog

java - Oracle EBS .ClassNotFoundException: oracle.apps.fnd.formsClient.FormsLauncher.class ERROR -

c# - how to use buttonedit in devexpress gridcontrol -

nvd3.js - angularjs-nvd3-directives setting color in legend as well as in chart elements -