I changed the definition of for-loop to a for loop to make it more readable to the eye.
getLetter(e) { var val = e.currentTarget.textContent; for (let letter of this.state.letters) { if (letter === val) { e.target.className = 'clicked'; letter.className = 'foundedLetter' } } }
This part of the code should work if only the letter variable you want to assign to the class Name is not a string character.
Make sure you also deploy spans. You can also look at this.refs and the link in your range. with this you can get a DOMElement and set the class name for it. I will show you how to use refs to get DOMelements:
var MyCom = React.createClass({ getInitialState: function() { return { random: 'lorem', letters:[], letter: '' } }, splitLetter:function(){ var s = this.state.random; for (var i = 0; i < s.length; i++) { this.state.letters.push(s.charAt(i)); } this.setState({ letters:this.state.letters }) }, getLetter:function(e){ var val = e.currentTarget.textContent; var _this = this; this.state.letters.forEach(function(letter) { if (letter === val) { alert("There is 'r' letter in the word.") console.log(letter, val); e.target.className = 'clicked'; _this.setState({letter: letter}) } }); }, render: function() { return ( <div> <p>The word is: <strong>{this.state.random}</strong></p> <p>Click the button first then click the r letter below</p> <button onClick={this.splitLetter}>Split letter</button> <p>Click on: <strong><span onClick={this.getLetter}>r</span></strong></p> {this.state.letters.map(function(item){ return ( <div> <span className={item == this.state.letter ? 'foundedLetter' : ''}>{item}</span> </div> ) },this) /*added this to map function */ } </div> ) } }); ReactDOM.render( <MyCom/>, document.getElementById("app") )
e.g. console.log (this.state.letters)
<span> H </span> → you can give it className
H → you cannot give className
source share