Parsing element on clicking a button

Parsing element on clicking a button



So I want to parse the h2 tag and save its text content into a constant after I the button "select" is clicked


<div className="card " + (this.state.selected ? "methodSelected" : "")>
<h2 className="charge"></h2>
<button onClick=this.selectMethod href="#"className="btn return-btn">SELECT</button>
</div>



The function I'm using to setState after clicking the button


selectMethod = () =>
this.setState(selected: true);






Can you show, please, what have you tried? In this way we can start a discussion from there...

– Andrea
Sep 12 '18 at 12:18




2 Answers
2



In the selectMethod method you could add -


selectMethod = () =>
this.setState(selected: true);
let x = document.getElementByClassName("charge").textContent;
// doing something with the variable 'x'





From my understanding of the question,this can be done in following way


selectMethod = () =>
document.getElementById('heading').innerHTML="sample text"
this.setState(selected: true);



Also add id attribute to to the heading tag as


id


<h2 id="heading"></h2>



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.