Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
71
rated 0 times [  78] [ 7]  / answers: 1 / hits: 18809  / 7 Years ago, fri, may 26, 2017, 12:00:00

I want to show a form onclick function for foreach post for a particular post id. I was trying like this but its not working.



 {% for post in posts %}

<div post-id={{ post.id }} class=post-box
w3-container w3-card-2 w3-white w3-round w3-margin><br>
{# <img src=w3images/avatar6.png alt=Avatar class=w3-left w3-circle w3-margin-right style=width:60px> #}
<span class=w3-right w3-opacity>{{ post.created }}</span>
<h4>{{ user.name }}</h4><br>
<hr class=w3-clear>
<p><a href=/posts/{{ post.id }}>{{ post.title }}</a></p>
{# <img src=w3images/nature.jpg style=width:100% class=w3-margin-bottom> #}
<p>{{ post.description }}</p>
<button type=button class=w3-button w3-theme-d1 w3-margin-bottom><i class=fa fa-thumbs-up></i>  Like</button>
<button class=w3-button w3-theme-d2 w3-margin-bottom id=comment><i class=fa fa-comment></i>Comment</button>
<div id=comment-form-{{ post.id }} style=display: none;>
<textarea></textarea>
<button type=button></button>
</div>
</div>

{% endfor %}


<script>
var postBoxes = document.getElementsByClassName('post-box')
postBoxes.forEach(function (postBox) {
var postId = postBox.getAttribute('post-id')
postBox.onclick = function () {
document.getElementById('comment-form-' + postId).style.display = 'block'
}
})
</script>

More From » javascript

 Answers
8

change your code like this declare the post-id inside the click event.And change selector withquerySelectorAll() .And also use with addEventListener() instead of normal click



Working example





var postBoxes = document.querySelectorAll('.post-box')
postBoxes.forEach(function(postBox) {

postBox.addEventListener('click', function() {
var postId = this.getAttribute('post-id')
console.log(postId)
document.getElementById('comment-form-' + postId).style.display = 'block'
})
})

<div post-id={{ post.id }} class=post-box
w3-container w3-card-2 w3-white w3-round w3-margin><br> {# <img src=w3images/avatar6.png alt=Avatar class=w3-left w3-circle w3-margin-right style=width:60px> #}
<span class=w3-right w3-opacity>{{ post.created }}</span>
<h4>{{ user.name }}</h4><br>
<hr class=w3-clear>
<p><a href=/posts/{{ post.id }}>{{ post.title }}</a></p>
{# <img src=w3images/nature.jpg style=width:100% class=w3-margin-bottom> #}
<p>{{ post.description }}</p>
<button type=button class=w3-button w3-theme-d1 w3-margin-bottom><i class=fa fa-thumbs-up></i> Like</button>
<button class=w3-button w3-theme-d2 w3-margin-bottom id=comment><i class=fa fa-comment></i>Comment</button>
<div id=comment-form-{{ post.id }} style=display: none;>
<textarea></textarea>
<button type=button></button>
</div>
</div>




[#57656] Tuesday, May 23, 2017, 7 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
bretd

Total Points: 6
Total Questions: 100
Total Answers: 97

Location: England
Member since Sun, May 21, 2023
1 Year ago
bretd questions
Sun, Jul 12, 20, 00:00, 4 Years ago
Sun, May 24, 20, 00:00, 4 Years ago
Mon, Jan 6, 20, 00:00, 5 Years ago
Mon, Jan 6, 20, 00:00, 5 Years ago
;