this is my html template:
<div class="posts">
<h3> <a href ="{% url 'profile' p.user.id %}"> {{p.user}}: </a> </h3> <p>{{p.timestamp}}</p>
<br>
<p><i class="fas fa-heart"></i> {{ p.likes.count }}</p>
<p style="text-align: center;"> {{ p.post }} </p>
{% if p.user.id == user.id %}
<button class="btn btn-primary edit" style="display: inline;">Edit</button><hr></div>
<div id="editsec">
<textarea rows=6 cols=100 style="opacity: 0.7;">{{p.post}} </textarea>
<form action=""><button class="btn btn-success">Save</button></form>
</div>
{% endif %}
{% endfor %}
Now in css I hide the editsec, so only if user clicks on edit button, it will display editsec div and hide the posts div. Here is Javascript code:
document.addEventListener('DOMContentLoaded', function() {
var edit = document.getElementsByClassName('edit')
for (var i = 0 ; i < edit.length; i++) {
edit[i].addEventListener('click', () => {
document.getElementById('editsec').style.display = "block";
document.querySelector('.posts').style.display = "none";
});
}
});
Now if I have two post in home page, if I click on second post's edit button, it is still displaying the first post's editsec div.
I tried to make this editsec a class and did this :
document.addEventListener('DOMContentLoaded', function() {
var edit = document.getElementsByClassName('edit')
var editsec = document.getElementsByClassname('editsec')
for (var i = 0 ; i < edit.length; i++) {
edit[i].addEventListener('click', () => {
for (var a = 0 ; a < edit.length; a++) {
editsec[a].style.display = "block";
document.querySelector('.posts').style.display = "none";
}
});
}
});
Now if I click on one edit button it will display all of the editsec div, How can I make it display only this particular posts' edit section?