I think your solution can use a little more tuning. Doing it this way creates (possible) many ajax requests that aren't really necessary.
You could just use the rel attribute:
<input type="submit" name="delete" value="Delete me" rel="Are you sure?" />
And then
$("input[type='submit'][name='delete']").click(function() {
return confirm($(this).attr('rel'));
});
Better yet, you could store a key in the rel attribute such as rel='confirmation.delete'
. And then have an interpreter such as:
var stringLibrary = {
prompt: {
foo: "Enter your foo:",
bar: "How many bars?"
},
confirmation: {
delete: "Are you sure you want to delete that?"
},
error: {
codes: {
324: "Oh no!",
789: "Oh yes!"
}
}
};
var trans = function(key) {
var result = stringLibrary;
key = key.split(".");
for ( var i in key ) {
if ( result[ key[i] ] ) {
result = result[ key[i] ];
} else {
return null;
}
}
return result;
}
Usage:
console.log(trans("prompt.foo"));
console.log(trans("confirmation.delete"));
console.log(trans("error.codes.324"));
console.log(trans("i.dont.exist"));
$("input[type='submit'][name='delete']").click(function() {
return confirm( trans($(this).attr('rel')) );
});