SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Guru johnjohn2's Avatar
    Join Date
    Apr 2004
    Location
    here
    Posts
    746
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    count of specific letters

    Is there a way to use javascript to count all the letter "d" in a specific table? I have a table of information on my page and I need some way of counting specific letters in the information.

    So the out put would be something like d=2

  2. #2
    do righteous deeds
    Join Date
    Aug 2006
    Location
    Türkiye Turkey تركيى
    Posts
    266
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
     
    <script type="text/javascript">
    
    function find(){
    var  el = document.getElementById('tableid');
    el = el.getElementsByTagName('td');
    var  td, i = 0, str ="";
    
    while(td = el[i++]) {
    str += td.innerHTML;
    } 
    alert(str);
    var c = "d";
    var n = str.match(RegExp('('+c+')','g')).length;
    alert(n);  // 6
    }
    </script>
    <input type="button" onclick="find()">
    <table id="tableid">
    <tr><td>abcdef</td><td>abcdef</td><td>abcdef</td></tr>
    <tr><td>abcdef</td><td>abcdef</td><td>abcdef</td></tr>
    </table>


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •