Reputation: 4502
I'm trying to apply styling through jquery in react component however i'm getting error Uncaught TypeError: this.getDOMNode is not a function
topicsVisited(arr){
$(function() {
$.each(arr, function(key, eachVisitedTopic) {
console.log(eachVisitedTopic);
$(this.getDOMNode()).find('.single-topic[data-topic-id="' + eachVisitedTopic + '"]').css({
'background-color': 'red'
});
});
});
};
Upvotes: 1
Views: 5968
Reputation: 2399
Try this
topicsVisited(arr){
var ReactDom = require('react-dom');
var self = this;
$(function() {
$.each(arr, function(key, eachVisitedTopic) {
console.log(eachVisitedTopic);
$(ReactDOM.findDOMNode(self)).find('.single-topic[data-topic-id="' + eachVisitedTopic + '"]').css({
'background-color': 'red'
});
});
});
};
Upvotes: 1
Reputation: 47182
You need to bind your functions in order to use this
properly.
topicsVisited(arr) {
$(function() {
$.each(arr, function(key, eachVisitedTopic) {
console.log(eachVisitedTopic);
$(this.getDOMNode()).find('.single-topic[data-topic-id="' + eachVisitedTopic + '"]').css({'background-color': 'red'});
}.bind(this));
}.bind(this);
}
or create a variable that references the correct this
.
topicsVisited(arr) {
var self = this;
$(function() {
$.each(arr, function(key, eachVisitedTopic) {
console.log(eachVisitedTopic);
$(self.getDOMNode()).find('.single-topic[data-topic-id="' + eachVisitedTopic + '"]').css({'background-color': 'red'});
});
};
}
Upvotes: 1