I try to highlight on my page the search expression with a button. Can anyone help me please to solve this problem?
Uncaught TypeError: Cannot read property 'indexOf' of undefined at highlight (?q=Python:11) at HTMLButtonElement.onclick (?q=Python:24)
The JS code:
function highlight(text) {
var titles = document.getElementsByClassName('title');
for (var x in titles) {
var innerHTML = titles[x].innerHTML;
var index = innerHTML.indexOf(text);
if (index >= 0) {
innerHTML = innerHTML.substring(0, index) + "<span class='highlight'>" + innerHTML.substring(index, index + text.length) + "</span>" + innerHTML.substring(index + text.length);
x.innerHTML = innerHTML;
}
}
}
Line 11:
var index = innerHTML.indexOf(text);
Line 24:
<button onclick="highlight('{{ search_expression }}')">Highlight search expression</button>