I am trying to generate a html code block when I click on a link. I have the same link “Reply” several times on the page. But I want to generate the code under the clicked link. I tried using $(this) but it did not work. Thanks for your help! If there is a better solution, please let me know. Also for reference, I am trying to create a reply textarea just like on Youtube when you click on the reply link it opens a textarea for the user to input text.
The sample code is available here Sample Code
Demo
Since there was a class
.replyon the target element, I used that instead of.normalText a:last-child.Instead of binding ‘click’ to the element,
.oneis used. Using.onewill only fire once which will prevent adding multiple textareas.$(this)refers to the element clicked which in this case is thea.replyelement. Using.parent()will travel up the DOM and select the parent which is aspan. And of course,.after(html)inserts the html after thespan.return false;does both.preventDefault();.stopPropagation();