Skip to content

Instantly share code, notes, and snippets.

@zaclittleberry
Created February 19, 2017 18:47
Show Gist options
  • Save zaclittleberry/326c460bcf14e1fb29a1d74baab969cb to your computer and use it in GitHub Desktop.
Save zaclittleberry/326c460bcf14e1fb29a1d74baab969cb to your computer and use it in GitHub Desktop.
A little script demonstrating a search form that will scan elements on a page (currently anchors) and highlight them if they contain the search phrase
<!DOCTYPE html>
<html>
<head>
<title>Highlighter</title>
<style>
.highlight {color: orange;}
a {color: blue; text-decoration: none;}
</style>
</head>
<body>
<form id="form" onsubmit="highlight(e)">
<input type="text" id="textbox"/>
<input type="submit" value="submit">
</form>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script>
$('#form').submit(function(e){
e.preventDefault();
highlight();
})
function highlight(){
console.log('working');
$('#content a').removeClass('highlight');
var search = $('#textbox').val();
$('#content a:contains('+search+')').addClass('highlight');
}
</script>
<div id="content">
<a href="#">one</a>
<a href="#">two</a>
<a href="#">three</a>
<a href="#">search</a>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment