jQuery plugin that doesn't work


/ Published in: jQuery
Save to your folder(s)



Copy this code and paste it in your HTML
  1. var grammar = {
  2. BracedTextRegex : new RegExp('\{.*\}')
  3. };
  4.  
  5. $('td.kanji').linkBracedText();
  6.  
  7. $.fn.linkBracedText = function() {
  8.  
  9. this.each(function() {
  10. // get text in curly-braces, abort if nothing
  11. var sentence = $(this).text();
  12. var $bracedText = sentence.match(grammar.BracedTextRegex);
  13. if ($bracedText === null) return;
  14. var bracedText = $bracedText[0].substr(1,$bracedText[0].length-2);
  15.  
  16. // replace curly-braced text with a span, which serves as an insertion point
  17. sentence = sentence.replace(grammar.BracedTextRegex,'<span></span>');
  18.  
  19. // find the 1st link (magically the 1 I need), change text to what was in curly braces
  20. var link = $(this).closest('tr').find('a:first').clone();
  21. link.text(bracedText);
  22.  
  23. //overwrite this element with a copy that has curly-braced replaced with appropriate link
  24. $(this).html(sentence).find('span').append(link);
  25. })};

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.