I am trying to do a simple search inside a static HTML page using jQuery. I should mention that this is only my first experience with jQuery.
I am trying to change the background of a found word on a page, and this is what I have tried so far:
myJavascript.js:
$(document).ready(function(){ $('#searchfor').keyup(function(){ page = $('#all_text').text(); searchedText = $('#searchfor').val(); $("p:contains('"+searchedText+"')").css("color", "white"); }); });
Here is also the HTML code:
page.html:
<html> <head> <title>Test page</title> </head> <body bgcolor="#55c066"> <input type="text" id="searchfor"></input> <p id="all_text">Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euism modo typi, qui nunc nobis videntur parum clari, fiant sollemnes in futurum. <font color="red">Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tinci futurum.</font> </p> </body> <script src="jquery-1.7.2.min.js"></script> <script src="myJavascript.js"></script> </html>
After checking the page with Firebug, I see that the variables in jQuery really get the value from the input field, but I assume that I messed up the highlighted part.
Thanks in advance for your help!
jquery html css search highlight
Radu gheorghiu
source share