I get uncaught typeerror cannot read property ‘top’ of undefined error when I run a JSFiddle working code in my application. The JSFiddle fine without any errors.
I can understand that the line in my code mentioned below ```
scrollTop = $('.highlighted:last', panel).position().top;
is unable to find the `highlighted` class as
console.log (“Check for highlighted in click handler:” +highlighted);
doesn't shows highlighted class getting applied to the document in my webapp. I suspect it is because of the following lines of code I am adding in my code:
for (var i = 0; i < length; i++) {
html = html.replace(new RegExp(records[i].concept_text, ‘ig’), ‘’ + records[i].concept_text + ‘’);
}
and when I check the console.log("How many span tags you are noticing here?: " + html);, I see the words (concept_text) are surrounded by two span tags like the following:
MYELOMA
And somewhere I noticed even three span tags.
Could it be the reason that the regular expression mentioned in the code
var
highlighted = content.replace(
regExp,
‘$1$2$3’
);
isn't applying the `span` tags because of already present two and three span tags?
Here is my code :
this.myDocument = function (data_) {
var source = {
localdata: data_,
datafields: [{
name: 'doc_content',
type: 'string'
}, {
name: 'concept_text',
type: 'string'
}, {
name: 'start',
type: 'int'
}, {
name: 'stop',
type: 'int'
}],
datatype: "array"
};
var dataAdapter = new $.jqx.dataAdapter(source, {
loadComplete: function (records) {
var html;
var color = '#FF0000';
//Get data
var records = dataAdapter.records;
var length = records.length;
console.log("Checking Length: "+length);// Outputs 5
for (var i = 0; i < length; i++) {
console.log("checking Concepts here: " +records[i].concept_text);
}
html = "<div style='margin: 10px;'><pre>" + records[1].doc_content + "</pre></div>";
for (var i = 0; i < length; i++) {
html = html.replace(new RegExp(records[i].concept_text, 'ig'), '<span style="color:' + color + ';">' + records[i].concept_text + '</span>');
}
console.log("How many span tags you are noticing here?: " + html);
$("#docContentPanel").html(html);
},
loadError: function (xhr, status, error) { },
beforeLoadComplete: function (records) {
}
});
// perform data binding
dataAdapter.dataBind();
$("#myPanel").jqxGrid({
source: dataAdapter,
width: '122',
height: '170',
columns: [{
text: 'Concept(s)',
datafield: 'concept_text',
cellsalign: 'center',
width: 100
}, {
text: 'Note Content',
datafield: 'doc_content',
hidden: true
}
]
});
var panel = $("#docContentPanel");
var content = panel.html();
panel.jqxPanel({
width: '750',
height: '500',
scrollBarSize: 20
});
$("#myPanel").on('cellclick', function (event) {
var value = event.args.value;
// Use a regular expression to account for the beginning/end of the
// input, arbitrary whitespace (including line feeds) as well as
// adjacent tags
var regExp = new RegExp('(^|>|\\s)(' + value + ')($|<|\\s)', 'g');
var scrollTop;
var highlighted = content.replace(
regExp,
'$1<span class="highlighted">$2</span>$3'
);
console.log ("Check for highlighted in click handler:" +highlighted);
panel.jqxPanel('clearcontent');
panel.jqxPanel('append', highlighted);
// Get the offset of the last highlighted word relative
// to the panel parent
scrollTop = $('.highlighted:last', panel).position().top;
// Use the jqxPanel API to scroll to that word
panel.jqxPanel('scrollTo', 0, scrollTop);
});
}; // End of myDocument function
Please advise. The original author of the above code who helped me comes from [this post.](https://www.sitepoint.com/community/t/dealing-with-character-offsets/236144/7)