I'm struggling for quite some time with the following question (that looks so simple). I want to change words between double curly brackets without breaking any events.
The difficulty lies in the fact that I don't know what events are set. So the background-color
on the span
is just an example, but could be anything.
This is my html:
<div class="test">foo <span>bar</span> {{ baz }}</div>
This is my JS:
$("span").
css('background-color', 'red').
click(function(){
alert('Clicked bar!');
});
$( ".test" ).
contents().
each(function(){
if( $(this).text().indexOf('{{') !== -1 )
{
$(this).
text().
replace(/{{\s?([^}]*)\s?}}/, "HOW DO I CHANGE {{ baz }} FOR SOMETHING ELSE WITHOUT BREAKING THE EVENT? THIS DOESN'T WORK");
}
});
Doing something like the example below wouldn't work because it breaks the action on the span
.
$('.test').
html(
$('.test').
html().
replace(/{{\s?([^}]*)\s?}}/, 'baq')
);