SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    do righteous deeds
    Join Date
    Aug 2006
    Location
    Türkiye Turkey تركيى
    Posts
    266
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    alert(el.innerHTML.split("new").length); // 6

    Code:
    <script type="text/javascript">
    
    function change(){
    var el = document.getElementById('tdid');
    alert(el.innerHTML.split("new").length); // 6 
    }
    </script>
    </head>
    
    <body><table width="100%"><tr><td id="tdid">
    new new new new new
    <br><br><br><a href="#" onClick="change()">change text</a></td></tr></table>
    There are 5 new. But message box displays 6. What can I do?

  2. #2
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    alert(el.innerHTML.split("new").length-1)

  3. #3
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The string you are splitting contains the html as well. Try alerting the array and you will see the contents.

    What do you want to achieve with this script?

  4. #4
    do righteous deeds
    Join Date
    Aug 2006
    Location
    Türkiye Turkey تركيى
    Posts
    266
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Mrhoo and Pepejeria...
    Code:
     
    <script type="text/javascript">
    
    function change(){
    var el = document.getElementById('tdid');
    for(var i = 0; i< 3; i++) {
    el.innerHTML = el.innerHTML.replace("eski","<b>eski</b>");
    }
    }
    </script>
    </head>
    
    <body><table width="100&#37;"><tr><td id="tdid">
    eski eski eski eski eski 
    <br><br><br><a href="#" onClick="change()">change text</a></td></tr></table>
    I want it display
    eski eski eski eski eski

    I write this:
    Code:
    <script type="text/javascript">
    var arr = [];
    function change(){
    var el = document.getElementById('tdid');
    var L = el.firstChild.nodeValue.split(' ');
    var fc = el.firstChild.nodeValue;
    alert(fc);
    alert(L.length); 6
    for(var i = 0; i< L.length-3; i++) { 
    alert(L[i])
    arr.push("<b>"+L[i]+"<b>"); }
    alert(arr)
    for(var n = 0; n < L.length -3; n++){
    fc = fc.replace(fc.split('')[n], arr[n])
    }
    }
    </script>
    </head>
    <body><table width="100%"><tr><td id="tdid">eski eski eski eski eski eski<a href="#" onClick="change()">change text</a></td></tr></table>
    It did not.
    The problem is resolved.
    Code:
    <HTML>
    <HEAD>
    <TITLE>Document Title</TITLE>
    
    <script type="text/javascript"> 
    function change(){
    
    el=document.getElementById('tdid')
    
    ar = el.innerHTML.split(" ")
    
    el.innerHTML = ""
    
    for(var i=0;i<ar.length;i++){
    
    if(i<3){
    el.innerHTML +="<b>"+ar[i]+"</b> "
    }
    else{
    el.innerHTML += ar[i]+" "
    }
    
    }
    
    } 
    
    </script> 
    </HEAD>
    <BODY>
    <table width="100%">
    <tr>
    <td> 
    <div  id="tdid">eski eski eski eski eski eski</div>
    <br><br><br>
    <a href="#" onClick="change()">change text</a>
    </td>
    </tr>
    </table> 
    
    </BODY>
    </HTML>
    Last edited by muazzez; Apr 23, 2007 at 10:32.


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
  •