Click event in reactjs for dynamic html elements
Below is the sample code for reactjs to click on dynamically created elements. You can get attribute values and can do much more.
document.querySelector('body').addEventListener('click', function(event) {
if (event.target.tagName.toLowerCase() === 'img') {
let urlStr=event.target.dataset.fullUrl;
if(urlStr.length>10){
window.open(urlStr, '_blank');
}
}
});
In the above code we are click in the img tag get attribute value of data-fullUrl and use it to open in a new window. You can use these attributes and values as per your requirement. Happy Coding 🙂