Is there a feature to enable a separate comment box for every tweet, similar to the toggle

Just starting out with jQuery and JS. Want to create a simplified version of Twitter with individual comment boxes for each tweet. Here's my JSFiddle: Currently, the comment box is appearing at the end of the last tweet instead of under each tweet. How can I achieve this?

Javascript:

$(document).ready(function () {
    $("#btn1").click(function () {
        $("p").append(" <b>Appended text</b>");
    });

    $(".commentbutton").click(function v() {
        var text = $("#txt1").val();

        $("body").append('<br><div class="x"><label class="gp">' + text + '</label><button id="b1"</button>Comment</div>');
        $("body").find(".x:last").hide().slideDown("slow");

    });

    $(document).on('click', '.x>button', function () {
        $('body').find('.x:last').append('<input id="txt1" type="text">');
    });
});

HTML:

<div style="position:absolute;left:230px;top:30px;border:solid;display:inline-block">
<p>Type a tweet</p>
<input id="txt1" type="text">
<br>
<button id="btn2" class="commentbutton">Tweet</button>
</div>

CSS:

.gp {
    border-style: solid;
    max-width: 30%;
    padding: 2px;
    display: block;
}
.x {
    width: 200px;
    background-color: #93bbd4;
    border-style: solid;
}
#txt1 {
    width: 200px;
    height: 28px;
}

Answer №1

Check out this link for a demo: Sample

$(document).on('click', '.x>button', function () {
    var id = "twt" + $(this).parent().index() +  "txt" + ($(this).parent().find('input').length+1);
    $(this).parent().append('<input id="' + id+ '" type="text">');
});

I have addressed the issue of adding text boxes dynamically. Additionally, I have resolved the problem with multiple ids that you were encountering.

Answer №2

To solve your issue, switch from using the append() method to the prepend() method. If I have correctly interpreted your question, this should resolve the problem.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Overlap of sub menus

Seeking assistance from a CSS expert for a challenge I am facing. I am currently working on a toggle menu for mobile view, and encountering issues with the submenu. Any list item placed below another one with children is not visible. Removing the parent l ...

Managing the width of an HTML table column based on its text content dynamically

Here is an example of an html table : <table width="100%" border="0" cellspacing="0" cellpadding="0"> <tr> <td width="480px" class="societe" ><div style="float:left;font-size:12px;padding-top:2px" ><?php echo $_SESSION ...

"Utilize Ajax to load PHP content and dynamically refresh a specific div

I have implemented an image uploading system, but now I want to incorporate a feature that allows users to rotate the uploaded images using Ajax. The challenge I'm facing is that if the session variable is lost during a full page update, I need to ens ...

Troubleshooting issue: `will_paginate` and jQuery integration not functioning properly due to non-responsive

I'm currently implementing a pagination feature in my application using jQuery, similar to how it's done on platforms like Facebook and Twitter. However, when I try to load "next" results, the entire HTML page is being loaded instead. It seems li ...

Scroll the div back to the top when the form is submitted

I have set up a form in a pop-up using Bootstrap modal. The form is quite long, so after submission, the message appears at the top of the form. However, I want it to scroll to the top when the user submits the form so they can easily see the message. Is ...

Exploring the mechanics behind jQuery selectors

Currently, I'm working on the Interactive website: Push menu training exercise on codecademy. However, I am a bit confused about jquery selectors. Specifically, in the example provided below, why does the 'menu' html element require a ' ...

The table loaded with Jquery/AJAX is not appearing on Internet Explorer 9

I'm encountering an issue with a table loaded via Jquery/AJAX. function initializeDaily() { jQuery.post( 'ajax-functions.php', { 'action':'getdailyStatus' }, function(respons ...

Select the input based on the name and value provided from a radio button using jQuery

I want to show a div element when the user chooses option 4 from a radio button. $(document).ready(function () { $("#GenderInAnotherWay").hide(); $("input[name='Gender'][value=4]").prop("checked", true); $("#GenderInAnotherWay").tog ...

The functionality of dragging and dropping list items with jQuery via AJAX is not functioning properly

After discovering that jquery drag and drop functionality cannot be applied to select dropdowns, I decided to create a dropdown using <ul> <li> elements. However, when I try dragging a list item, I want to display the corresponding highchart th ...

An innovative countdown clock for WooCommerce that dynamically displays delivery time as a customizable shortcode

After successfully creating a Wordpress shortcode with a JavaScript counter, I encountered an issue: Back End - Counter functions properly: https://i.stack.imgur.com/qyHIL.png Front End - Counter not working (no console errors...): https://i.stack.imgu ...

What is the best way to selectively add multiple classes to a single Material UI classes attribute based on certain conditions?

I am trying to use the Material UI Drawer and wish to add a class named "paperStyle" to the classes prop with the rule name "paper" and then, under certain conditions, apply an additional class called "specialPaperStyle" for specific users. However, I have ...

Enhancing Your Navbar Brand with Images using Flexbox and Bootstrap

I've been working on a project where I decided to customize the Bootstrap navbar by breaking it up into flexboxes in order to align everything according to the specifications. However, I've encountered an issue while trying to place a logo in the ...

Combining the power of two distinct jQuery libraries

I am currently using jQuery version 1.9.1 and jQuery1 version 1.2.6 in the code below. How can I successfully integrate both versions to ensure compatibility? <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1 ...

Create an interactive mechanism where one scrollbar dynamically adjusts another scrollbar and vice versa

Using the provided js-bin, how can I synchronize scrolling between the "left" div and the "right" div when using the mouse wheel? Currently, if you uncomment the code block, the scrolling behavior changes from scrolling by 100px per scroll to scrolling pi ...

What could be causing the video not to display in landscape mode on the iPad Pro?

I'm having an issue with a standard HTML5 <video> on my website. The videos are working fine on most devices, however there is a problem specifically with the iPad Pro in landscape orientation. It seems to work properly in portrait orientation ...

Preventing Body Overflow Without Affecting Iframe Overflow in Meteor.js

Currently, I am working on a project using Meteor for a Point of Sale (POS) system. For those unfamiliar with it, Meteor is a framework that allows for the use of JavaScript, jQuery, and various other web app scripting languages. The goal of this applicati ...

Using JSON / jQuery: How to Handle XML Data in the Success Function of an AJAX Post Request

Greetings! I am currently performing an ajax post through a JSP. The JSON data is being sent in string format (parsed using parseJSON, then converted back to a string using JSON stringify). The post operation functions correctly. However, my query lies in ...

What is the best way to transmit a JSON object from a Python code to jQuery?

After searching through various APIs and resources, I'm still struggling to properly fetch a JSON object from a Python script using AJAX. It seems like the issue lies in how I am handling the JSON object. To begin, I have a python script on my server ...

Managing and comparing category IDs in JavaScript to effectively store and render subcategories

My goal is to set the current category ID in a variable, and if the category changes, I want to store that as well. Then, I need to compare both IDs. If they are not equal, I want to set the subcategory to null. However, I am unsure of where my mistake lie ...

How can a JQuery slideshow be programmed to only iterate once?

Looking to create a slideshow that transitions between images every two seconds? Check out the code snippet below: HTML: <div class="fadeIn"> <img src="img/city.png" class="remimg" id="city"> <img src="img/shop.png" class="remimg" ...