I ashamed and frustrated by this question. I’m writing what should be a pretty simple script that removes “y”s and replaces them with “i”s. When this happens, I add a with a class to the new “i” so that I can undo it. For some reason, the first replacement works… the switch back works… it works AGAIN… then stops. I don’t know what else to do but just post the whole ugly script.
Don’t worry about the gnarly regex expression (it’s targeting only certain “y”s which are sometimes used for writing Mohawk).
EDIT: Here’s a live version on jfiddle: http://jsfiddle.net/vkVBk/
$(document).ready(
function() {
function swap_out_y(x) {
//alert(x);
x = x.replace(/y/, "i");
x = x.replace(/Y/, "I");
alert('swap_out_y just ran. x = '+x);
return x;
}
function swap_out_i(x) {
//alert(x);
x = x.replace(/i/, "y");
x = x.replace(/I/, "Y");
alert('swap_out_i just ran. x = '+x);
return x;
}
$('body').delegate('.y_to_i', 'click', y_to_i);
//$('body').delegate('.i_to_y', 'click', i_to_y);
function y_to_i() {
$("span.mohawk_word").each(
function() {
$(this).html(
$(this).text().replace(/(y[aevu])|(y[oe]n)/ig, function(s) {
return '<span class="consonant">'+swap_out_y(s)+'</span>';
})
);
}
);
$('.y_to_i').undelegate('click');
$('.y_to_i').addClass('i_to_y');
$('.y_to_i').removeClass('y_to_i');
$('body').delegate('.i_to_y', 'click', i_to_y);
//alert('A');
} //end y_to_i
function i_to_y(){
$("span.consonant").each(
function() {
$(this).html($(this).text().replace(/i/ig,
function(s) {
return swap_out_i(s);
}
)//replace...
);//html
//$(this).removeClass('replacement_i');
}//function(){
);//each(
$('.i_to_y').undelegate('click');
$('.i_to_y').addClass('y_to_i');
$('.i_to_y').removeClass('i_to_y');
$('body').delegate('.y_to_i', 'click', y_to_i);
}
} //function
); //document ready
You’re calling delegate on ‘body’ and then calling undelegate on some classes instead of body. So you’re ending up with multiple delegates.
Working fiddle. http://jsfiddle.net/5HdSC/1/