Connecting Tech Pros Worldwide Forums | Help | Site Map

Wrap inner table cell text with mailto...

Newbie
 
Join Date: Aug 2007
Posts: 10
#1: Feb 3 '08
hi,
i'm tring to get access to a cell inside a table that has the vlaue of an Email address but is not clickable.
i tring to go to that cell and Wrap the inner text with <a href="mailto... tag so it would be clickable.
i tried to use this code:
x=document.getElementById('GridView1').childNodes[14].childNodes[1];
but with no luck,for some resone i can't access the cell and it's text.

need you help on doing so...

Needs Regular Fix
 
Join Date: Jun 2006
Posts: 424
#2: Feb 3 '08

re: Wrap inner table cell text with mailto...


You may find it useful to use a general method for processing text-
deepText will pass all the text nodes that are descendants of a specified parent
to a specified function. Something like a recursive forEach method.

wrapTableMail is set up to process every table on the page, looking for valid email text, but you could specify one table,or one row to start from.

It will find common emails(joesmith@website.com), but you'll probably want a better Rx filter.

Expand|Select|Wrap|Line Numbers
  1. function deepText(hoo, fun) {
  2.     var A= [], next, T, pa, i;
  3.     if(!hoo) return A;
  4.     if(hoo.nodeType== 3) A.push(fun(hoo));
  5.     else if(hoo.hasChildNodes()) {
  6.         pa= hoo.childNodes, i= 0;
  7.         while(pa[i]){
  8.             next= pa[i++];
  9.             T= next.nodeType;
  10.             if(T== 3){
  11.                 if(/\w+/.test(next.data)) A.push(fun(next));
  12.             }
  13.             else if(T== 1) A= A.concat(arguments.callee(next, fun));
  14.         }
  15.     }
  16.     return A;
  17. }
Expand|Select|Wrap|Line Numbers
  1. function wrapTableMail(){
  2.     var T= document.getElementsByTagName('table');
  3.     var wrapN= document.getElementsByTagName('a').length;
  4.     var L= T.length, who;
  5.     var fun= function(hoo){
  6.         var Rx= /\b([\w]+(\.[\w-]+)*@([\w-]+\.)[a-z]{2,6})\b/i
  7.         if(hoo.data){
  8.             var pa= hoo.parentNode;
  9.             if(pa.nodeName== 'A') return;
  10.             var res,el;
  11.             var str= hoo.data;
  12.             var X= document.createElement('a');
  13.             X.className= 'wrapMailCss';
  14.             while(str && (res= Rx.exec(str)) != null){
  15.                 var tem= res[1];
  16.                 el= X.cloneNode(false);
  17.                 el.href= 'mailto:'+tem;
  18.                 el.appendChild(document.createTextNode(tem));
  19.                 hoo.replaceData(res.index,tem.length,'');
  20.                 hoo= hoo.splitText(res.index);
  21.                 str= hoo.data;
  22.                 if (str) pa.insertBefore(el, hoo);
  23.                 else pa.appendChild(el);
  24.                 Rx.lastIndex= 0;
  25.             }
  26.         }
  27.     }
  28.     while(L){
  29.         who= T[--L];
  30.         deepText(who,fun)
  31.     }
  32.  
  33.     // the rest is just success testing code
  34.     wrapN=document.getElementsByTagName('a').length-wrapN;
  35.     var s= (wrapN)+ ' mailto link';
  36.     if(wrapN>1) wrapN+= 's';
  37.     alert(s+' added');
  38. }
//call the function
wrapTableMail();
Reply


Similar JavaScript / Ajax / DHTML bytes