/    Sign up×
Community /Pin to ProfileBookmark

Text Highlighting Help

I have an HTML table that is dynamically built and populated with data from a database. I want to incorporate a text search on the page so that when a user types a search term every term within a DIV that wraps the table is highlighted on a character-by-character basis.

Conversely, as the user deletes characters from the input search field, the highlights are recalculated to omit the deleted characters.

I have a script that I found online that works, but in some cases it inserts “ ” and “&” strings as I type in the input box and I cannot for the life of me figure out why.

Any pointers, code samples, links, etc. are appreciated.

Thanks for reading!

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@JMRKERApr 12.2013 — Why ask for help if you don't show us the code that is giving you problems?

?
Copy linkTweet thisAlerts:
@lenglish01authorApr 12.2013 — I forgot to add it, sorry.

I found this online and for the most part, it works, however sometimes for some reason it inserts " " when entering a search term and "&" when I hit the delete key.

[CODE]function filterTable(Stxt, table) {
dehighlight(document.getElementById(table));
if (Stxt.value.length > 0)
highlight(Stxt.value.toLowerCase(), document.getElementById(table));
}

function dehighlight(container) {
for (var i = 0; i < container.childNodes.length; i++) {
var node = container.childNodes[i];
if (node.attributes && node.attributes['class'] && node.attributes['class'].value == 'highlighted') {
node.parentNode.parentNode.replaceChild(
document.createTextNode(node.parentNode.innerHTML.replace(/<[^>]+>/g, "")),node.parentNode);
return;
} else if (node.nodeType != 3) {
dehighlight(node);
}
}
}

function highlight(Stxt, container) {
for (var i = 0; i < container.childNodes.length; i++) {
var node = container.childNodes[i];
if (node.nodeType == 3) {
var data = node.data;
var data_low = data.toLowerCase();
if (data_low.indexOf(Stxt) >= 0) {
var new_node = document.createElement('span');
node.parentNode.replaceChild(new_node, node);
var result;
while ((result = data_low.indexOf(Stxt)) != -1) {
new_node.appendChild(document.createTextNode(data.substr(0, result)));
new_node.appendChild(create_node(
document.createTextNode(data.substr(result, Stxt.length))));
data = data.substr(result + Stxt.length);
data_low = data_low.substr(result + Stxt.length);
}
new_node.appendChild(document.createTextNode(data));
}
} else {
highlight(Stxt, node);
}
}
}

function create_node(child) {
var node = document.createElement('span');
node.setAttribute('class', 'highlighted');
node.attributes['class'].value = 'highlighted';
node.appendChild(child);
return node;
}[/CODE]



Thanks!
×

Success!

Help @lenglish01 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...