javascript - Get the first number value from a descendant inside a table -


i'm trying way first number value present inside table (and respective tbody), needs able find value first number, , ignores tags comes accross until reaches number value.

<table id="tableid">     <thead></thead>     <tbody>         <tr></tr>         <tr>             <td></td>             <td>                 <div>                     <div>                         <span>                             4031007                         </span>                     </div>                     <div>                         <span>                             whatever                         </span>                     </div>                 </div>             </td>             <td></td>         </tr>     </tbody> </table> 

in above example, try find 4031007, inside <span>, could've been <div> or else. need without using jquery. help?

you plain old way: make recursive function return text of first node has text content:

function findfirstnumber(node) {     // if text node, return contents. trim because there     // whitespace between elements should ignored     if (node.nodetype == node.text_node)         return node.textcontent.trim();      // iterate on child nodes , finde first 1 has text in     (var child = node.firstchild; child; child = child.nextsibling) {         var content = firsttext(child);         if (content && isnumber(content))             return content;     }      // no text found     return ''; }  function isnumber(value) {     return !!isnan(value); }  console.log(findfirstnumber(document.getelementbyid('tableid'))); 

i used mdn page node find out how this.

see fiddle (open console)


Comments

Popular posts from this blog

javascript - how to protect a flash video from refresh? -

visual studio 2010 - Connect to informix database windows form application -

android - Associate same looper with different threads -