Skip to content

Instantly share code, notes, and snippets.

@witmin
Last active October 24, 2017 00:34
Show Gist options
  • Save witmin/9c51677fc6d7826a0b57d7417297894e to your computer and use it in GitHub Desktop.
Save witmin/9c51677fc6d7826a0b57d7417297894e to your computer and use it in GitHub Desktop.
A simple jQuery function to insert a <span> with 'lang' attribute to a different language text for font decoration
/**
* @description function to replace text with a English language attribute
* @param {string} element - html element that need to be check and updated
* @param {string} text - text to be replaced
* @param {string} langCode - the language attribute that needs to be included.
*/
function insertLangSpan(element, text, langCode) {
var newHTML = '<span lang="' + langCode + '">'+ text +'</span>';
$(element).html(function (index, html) {
return html.replace(text, newHTML);
});
}
/**
* @description call functions on page load
*/
$(function () {
// Example, on a page in Thai, select specific English words and add <span lang="en" to specific so that we can assign and English font to it for better display
insertLangSpan('html[lang="th"] h1', 'A cat', 'en');
insertLangSpan('html[lang="th"] h2', 'A dog', 'en');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment