First you must decide exactly what criteria you will use for selecting a replacement -- I would suggest doing it on a word boundary, such that "I work with WHO" will wrap "WHO" in an abbr, but "WHOEVER TOUCHED MY BIKE WILL REGRET IT" won't abbreviate "WHO". You should also decide if you are going to be case sensitive (probably you want to be, so that "The guy who just came in" doesn't abbreviate "who". ) Use jQuery to recurse over all of the text in the document.
This can be done using the children selector and stepping through elements and reading all the text For each text node, split the text into words For each word, look it up in your key value store to see if it matches a key. If so, get the value, and construct a new element abbr title="value">key.
First you must decide exactly what criteria you will use for selecting a replacement -- I would suggest doing it on a word boundary, such that "I work with WHO" will wrap "WHO" in an abbr, but "WHOEVER TOUCHED MY BIKE WILL REGRET IT" won't abbreviate "WHO". You should also decide if you are going to be case sensitive (probably you want to be, so that "The guy who just came in" doesn't abbreviate "who". ) Use jQuery to recurse over all of the text in the document.
This can be done using the . Children selector and stepping through elements and reading all the text. For each text node, split the text into words.
For each word, look it up in your key value store to see if it matches a key. If so, get the value, and construct a new element key. Break up the text node into a) the text before the abbreviation (a text node), b) the abbreviation itself (an element), and c) the text after the abbreviation (a text node).
Insert all three as child nodes of the original text node's parent, replacing the original text node. Each of these steps will require a bit of work and looking up some API docs, but that is the basic process.
Thanks for the start - I'll never really be using 'WHO' (or other abbreviations that could be used as, or in, words) but you bring up exactly the sort of questions I was hoping to hear. – Aaron Silber Aug 11 at 1:54.
Firstly, this should really be done on the server, doing it on the client is very inefficient and much more prone to error. But having said that... You can try processing the innerHTML of the element, but javascript and regular expressions are really bad at that. The best way is to use DOM methods and parse the text of each element.
When a matching word is found, replace it with an abbr element. This requires that where a match is found in a text node, the entire node is replaced because what was one text node will now be two text nodes (or more) either side of an abbr element. Here is a simple function that goes close, but it likely has foibles that you need to address.It works on simple text strings, but you'll need to test it thoroughly on more complex strings.
Naturally it should only ever be run once on a particular node or abbreviations will be doubly wrapped. Var addAbbrHelp = (function() { var abbrs = { 'WHO': 'World Health Organisation', 'NATO': 'North Atlantic Treaty Organisation' }; return function(el) { var node, nodes = el. ChildNodes; var word, words; var adding, text, frag; var abbr, oAbbr = document.
CreateElement('abbr'); var frag, oFrag = document. CreateDocumentFragment() for (var i=0, iLen=nodes. Length; I This is the WHO and NATO string.
Some non-NATO forces were involved. And calling: addAbbrHelp(document. GetElementById('d0')); results in (my formatting): This is theWHO and NATO string.
Some non-NATO forces were involved. Using the word break pattern to split words is interesting because in strings like "with non–NATO forces", the word NATO will still get wrapped but not the "non–" part. However, if the abbreviation is split across a text node or by a hyphen, it will not be recognised unless the same pattern is included as a property name in the abbrs object.
Check out the javascript replace method. I'd use JQuery to pull out all the text in the paragraph var text = $(p#paragraphId).html() Use a for loop to loop through the list of abbreviations you have and then use the replace() method mentioned above to swap out the abbreviation for the tag you need. Finally use JQuery to set the html of the paragraph back to your newly updated string.
Var sentence = 'The WHO eLENA clarifies guidance on life-saving nutrition interventions, and assists in scaling up action against malnutrition,' var words = sentence. Split(' '); for (var I = 0; I ' + wordsi + ''; var newSentence = words. Join(' ').
The question said it has a list of abbreviations in a key-value dictionary. Also your code doesn't put anything in the title attribute. – mgiuca Aug 11 at 1:48 also what if the text was sentences what if there was something like WHO, no-1 though.
– naveen Aug 11 at 1:51.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.