replace - Replacing multiple text strings in blocks of text using jquery.each() -


i'm trying create nested loops apply clickable definitions words within several blocks of texts. first, outer loop iterates through definition names , ids in each class 'n-concept' , creates new <span> used inner loop. inner loop goes through each block of 'original-text' , replaces each instance of 'nounname' <span> template created in first loop. here's code...

$(document).ready(function(){        $('.n-concept').each(function(){                 var nounname = $(this).find('h3').html();         var nounid = $(this).attr('id');         var newstring = '<span data-vocabid="'+nounid+'" class="noun-name">'+nounname+'</span>';          $('.original-text').each(function(){             var newtext = $(this).text().replace(regexp(nounname, 'gi'), newstring);             $(this).html(newtext);                           });     }); }); 

currently, loops working values last iteration of 'n-concept' loop being applied, i.e., it's not preserving changed value of original text. i'm bit new javascript , jquery forgive me if simple problem solve. reading up, seems closure might need here? appreciated :)

you should use html() method instead of text(), because text() method ignores spans add in previous iteration.

http://jsfiddle.net/erztfr5s/


Comments

Popular posts from this blog

javascript - Chart.js (Radar Chart) different scaleLineColor for each scaleLine -

apache - Error with PHP mail(): Multiple or malformed newlines found in additional_header -

java - Android – MapFragment overlay button shadow, just like MyLocation button -