Highlight text as you type.

Discussion in 'PHP' started by Shimurai, Jun 26, 2010.

  1. #1
    Hello,

    I have a page with a big table full of information and I would like to add something like a search from, but what I need is that it will only highlight the text from the current page text.

    something like using CONTROL + F on firefox..

    any way to do this?

    Thanks !!
     
    Shimurai, Jun 26, 2010 IP
  2. Kaimi

    Kaimi Peon

    Messages:
    60
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You can try something like this for table:
    
    <html>
    <script>
    function search()
    {
    	var search = document.getElementById('text').value;
    	var table = document.getElementById('data');
    	var cells = table.getElementsByTagName('td');
    	
    	for(i = 0, j = cells.length; i < j; i++)
    		if(cells[i].innerHTML.indexOf(search) > 0)
    			cells[i].style.backgroundColor = 'red';
    		else
    			cells[i].style.backgroundColor = 'transparent';
    }
    </script>
    <input type="text" id="text" onChange="javascript:search()">
    <table id="data" border="1">
    <tr>
    <td>value1</td>
    <td>value2</td>
    </tr>
    <tr>
    <td>value3</td>
    <td>value4</td>
    </tr>
    </table>
    </html>
    
    Code (markup):
     
    Kaimi, Jun 26, 2010 IP
  3. Shimurai

    Shimurai Well-Known Member

    Messages:
    186
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    110
    #3
    uhmm.. I just tried that code but it didn't do anything at all..
     
    Shimurai, Jun 26, 2010 IP
  4. Kaimi

    Kaimi Peon

    Messages:
    60
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #4
    And this?
    
    <html>
    <script>
    function search()
    {
    	var search = new RegExp(document.getElementById('text').value, "i");
    	var table = document.getElementById('data');
    	var cells = table.getElementsByTagName('td');
    	
    	for(i = 0, j = cells.length; i < j; i++)
    		if(cells[i].innerHTML.match(search))
    			cells[i].style.backgroundColor = 'red';
    		else
    			cells[i].style.backgroundColor = 'transparent';
    }
    </script>
    <input type="text" id="text" onChange="javascript:search()">
    <table id="data" border="1">
    <tr>
    <td>value1</td>
    <td>value2</td>
    </tr>
    <tr>
    <td>value3</td>
    <td>value4</td>
    </tr>
    </table>
    </html>
    
    Code (markup):
     
    Kaimi, Jun 27, 2010 IP
  5. Shimurai

    Shimurai Well-Known Member

    Messages:
    186
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    110
    #5
    Hi Kaimi,

    your code worked just perfectly, however there are two things that i would like to know if it's possible to change:

    1.- If I search for nothing it will mark the whole table.
    http://geupload.com/images/img1.jpg

    2.- When there's a match, it will only mark the TD's that have the word I've searched, is it possible to make it select the whole TR?
    http://geupload.com/images/img2.jpg

    Thanks a lot for your help!
     
    Shimurai, Jun 28, 2010 IP
  6. Kaimi

    Kaimi Peon

    Messages:
    60
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #6
    
    <html>
    <script>
    function search()
    {
    	var search = new RegExp(document.getElementById('text').value ? document.getElementById('text').value : '^$', 'i');
    	var table = document.getElementById('data');
    	var cells = table.getElementsByTagName('td');
    	
    	var tmp = -1;
    	for(i = 0, j = cells.length; i < j; i++)
    		if(cells[i].innerHTML.match(search) && tmp != cells[i].parentNode.rowId)
    		{
    			cells[i].parentNode.style.backgroundColor = 'red';
    			tmp = cells[i].parentNode.rowId;
    		}
    		else if(tmp != cells[i].parentNode.rowId)
    			cells[i].parentNode.style.backgroundColor = 'transparent';
    		else
    			tmp = -1;
    }
    </script>
    <input type="text" id="text" onChange="javascript:search()">
    <table id="data" border="1">
    <tr>
    <td>value1</td>
    <td>value2</td>
    </tr>
    <tr>
    <td>value3</td>
    <td>value4</td>
    </tr>
    <tr>
    <td>value1</td>
    <td>value2</td>
    </tr>
    </table>
    
    Code (markup):
     
    Kaimi, Jun 29, 2010 IP
  7. Shimurai

    Shimurai Well-Known Member

    Messages:
    186
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    110
    #7
    Hello Kaimi,

    it doesn't seems to be working with the last code you gave me, but the older one it does..

    any idea why this could be?

    Thanks !
     
    Shimurai, Jul 2, 2010 IP
  8. Kaimi

    Kaimi Peon

    Messages:
    60
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #8
    No idea, works fine in opera 10.53 and ff 3.6.6
     
    Kaimi, Jul 3, 2010 IP