Skip to content
Advertisement

Hiding “write reply” textfield under each comment

I’m working on my school project which is gallery, a clone of flickr and unsplash really. Right now I was working on comments system which works rather well all things considered, but I also want to add replies under those comments and having textfields under each comment seems clunky, so I wrote small jQuery script for it:

This is test file for this part of the code, as I don’t want to ruin something in the main project if something happens. PHP/HTML

JavaScript

jQuery

JavaScript

This kinda works, but while($row = mysqli_fetch_assoc($result)), just spins through all id’s and stops at the last one, so all reply buttons only work on the last comment. Question is how do I apply this script to all comments, instead of only the last so that reply buttons show textfields that are appropriate?

Advertisement

Answer

Instead of writing jquery code for all button individually you can write your event handler like this $("button[class*=replybtn_]").click(function() { so this event will get called when any button has class i.e :replybtn and anything after that word. As you have use _ to attach id as well you can use split("_")[1] this will give you value after _ then using this you can toggle your reply div.

Demo Code:

JavaScript
JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement