Jquery - another novice question on hiding

Hi,
Again a novice type question, I have the attached code. The table I want to search has an id of #prices_table. and I have a search box called #txtSearch.
My overall goal is for all tr's which don't contain the search term to be hidden, but I cant even get the text to change color for the appropriate lines. Any help with either of these problems would be fantastic.

Cheers
John
jQuery("#txtSearch").keyup(function() {
    jQuery('#prices_table').css('color','black');
    val=jQuery("#txtSearch").val();
    jQuery('#prices_table').find(":contains('"+val+"')").css('color','blue');
});

Open in new window

LVL 2
jdav357Asked:
Who is Participating?
 
Steve KrileCommented:
I really love problems like this.  On the surface this seems simple...just highlight the words I'm searcing for! This is NOT trivial though.  I've attached a working sample, but there are some things to consider.

If there are html tags in the TD's you are searching this WILL NOT WORK as expected.  You will likely mess up the html with this approach.

But, this DOES work if the cells of your table contain plain text.

Conceptually, here's what is happening.

You have a style called highlight that has a yellow background.
There is a search box that we will wire up a keyup event using jquery.
There is a table that we will "loop" through using the .each() function to look for matches.

First, when looping through the cells, we clear out any previously entered highlight tags.

Then, if we find a match (using regular expression pattern matching) we "wrap" the match in a span tag with the highlight class.

As the user changes the text in the box, words in the table are highlighted if they match.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style>
.highlight {background-color:yellow;}

</style>
<script language="javascript" src="http://code.jquery.com/jquery-1.4.2.min.js"></script>

</head>
<body>

	<input type="text" id="search" />
	<table id="searchTable">
		<tr>
			<td>This is some text</td>
			<td>This is more text</td>
		</tr>
		<tr>
			<td>I have more info here</td>
			<td>And still more to see here</td>
		</tr>
		<tr>
			<td>Sample text that you could search too</td>
			<td>random words and such</td>
		</tr>
	</table>


<script language="javascript">
		var spanexp = new RegExp("<span class=\"highlight\"[^>]*>(.*?)</span>","i")
        $(document).ready(function() {
			//wire the keyup function to the search box
        	$("#search").keyup(function(){
				//get the value of the search box
        		var term = $("#search").val();

        		//execute the highlighting function
				doSearch(term);

        	});

		});

		function doSearch(term){
			//create a regular expression for our replace function
			var reg = new RegExp(term,"g");

			//loop through each td tag in the table being searched
			$("#searchTable tr > td").each(function(i){
				$(this).text($(this).text().replace(spanexp,""))
				//replace any matched text with our span highlight
				$(this).html($(this).text().replace(reg, '<span class="highlight">'+term+'</span>'));
			})



		}



</script>

</body>
</html>

Open in new window

0
 
StealthyDevCommented:
If you can send your HTML + Script, we can solve it quicker!
0
 
Steve KrileCommented:
Let me know if you see how this is working...if so, we can move to the next step of hiding/showing matching rows.
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
hieloCommented:
assuming there is a header row, try:
<script>
$(function(){
     jQuery("#txtSearch").keypress(function() {
		setTimeout(function(){
	         jQuery('#prices_table').css('color','black');
	         var val=jQuery("#txtSearch").val();
		    jQuery('#prices_table tr:gt(0)').not(':contains('+val+')').closest('tr').css('color','blue');
		},1);
     });
});
</script>

If there is no header then get rid of ":gt(0)" in the "tr:gt(0)" expression

Open in new window

0
 
jdav357Author Commented:
Many thanks to all for your assistance.
skrile - Thank you very much for the code example. from what I read you have hit the nail on the head. I am not at the development machine right now, so can't try it out but it does look like what i'm after.

I will try tomorrow and get back to you if I can, if this is ok.

Cheers
John
0
 
hieloCommented:
I thought you wanted to hide the non-matches (and reveal them again if the input is erased)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<title>hielo</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
<script>
$(function(){
     jQuery("#txtSearch").keypress(function() {
		setTimeout(function(){
	         jQuery('#prices_table').css('color','black');
	         var val=jQuery("#txtSearch").val();
		    if( val )
		    		jQuery('#prices_table tr:gt(0)').not(':contains('+val+')').closest('tr').hide();
		    else
		    		jQuery('#prices_table tr').show();
		},1);
     });
});
</script>
	</head>
	<body>
	<table id='prices_table'>
		<tr><td>Description</td><td>Price</td></tr>
		<tr><td>Shirt</td><td>$32.00</td></tr>
		<tr><td>Shoes</td><td>$52.00</td></tr>
		<tr><td>Pants</td><td>$42.00</td></tr>
	</table>
	<input id='txtSearch' value='' type='text'/>
	</body>
</html>

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.