Use jQuery to insert items into the text box

When I drag li elements into the textbox, they are arranged in the middle. How can I arrange these elements at the beginning? I am using Foundation.

Is there a possible issue?

This is my jQuery code:

<script type="text/javascript">
    $(function() {
        $("#dragdiv li").draggable({
            helper: "clone",
            cursor: "move",
            revert: "invalid"
        });

        initDroppable($("#dropdiv"));
        function initDroppable($elements) {
            $elements.droppable({
                activeClass: "ui-state-default",
                hoverClass: "ui-drop-hover",
                accept: ":not(.ui-sortable-helper)",

                over: function(event, ui) {
                    var $this = $(this);
                },
                drop: function(event, ui) {
                    var $this = $(this);
                    if ($this.val() == '') {
                        $this.val(ui.draggable.text());
                    } else {
                        $this.val($this.val() + "," + ui.draggable.text());
                    }
                }
            });
        }
    });
</script>

<style type="text/css>
    #dropdiv ul
    {
        display: inline-block !important;
        float: left;
    }
    #dragdiv ul{
        margin-top:20px; 
    }

Here is the HTML:

<p><b>Tags:</b></p>

<input id="dropdiv" type="text" style="overflow:scroll;width:605" />

<div id="dragdiv">
    <?php if($tags): ?>
    <ul id="allItems">
        <?php foreach($tags as $t): ?>
        <li id="node" runat="server">
            <?php echo $t['name'] ?>
        </li>
        <?php endforeach; ?>
    </ul>
    <?php endif; ?>  
</div>  

Can someone assist me with this? Please help...

<p><b>Tags:</b></p>

<input id="dropdiv" type="text" style="width:605" />

<div id="dragdiv">
    <ul id="allItems">
        <li id="node" runat="server">
            first tag                           </li>
        <li id="node" runat="server">
            second tag                          </li>
        <li id="node" runat="server">
            third tag                           </li>
    </ul>

</div>                

Answer №1

You have multiple tabs within the li tags, surrounding the text, and they are also being duplicated.

To resolve this issue, modify the PHP code to eliminate any tabs...

<li id="node" runat="server"><?php echo $t['name'] ?></li>

Alternatively, you can adjust the drop function like so...

drop: function (event, ui) {
    var $this = $(this);
    if ($this.val() == '') {
        $this.val(ui.draggable.text().trim());
    } else {
        $this.val($this.val() + "," + ui.draggable.text().trim());
    }
}

I've included trim() to remove any leading or trailing whitespace.

Check out a working example on jsfiddle

Answer №2

I have made some changes to your code in order to append items to the list.

HTML

<h2>Start Dragging Here</h2>
<div id="dragZone">
    <ul id="allItems">
        <li class="node" runat="server">Item 1</li>
        <li class="node" runat="server">Item 2</li>
        <li class="node" runat="server">Item 3</li>
    </ul>
</div> 

<h2>Drop Area</h2>
<input id="dropArea" type="text" style="width:605" />

JQuery

        $("#dragZone li").draggable({
            helper: "clone",
            cursor: "move",
            revert: "invalid"
        });

        $( "#dropArea" ).droppable({
                activeClass: "ui-state-default",
                hoverClass: "ui-drop-hover",
                accept: ":not(.ui-sortable-helper)",
                drop: function(event, ui) {
                    var $target = $(this);
                    if($target.val().indexOf(ui.draggable.text()) < 0 ) {
                        if ($target.val() == '') {
                            $target.val(ui.draggable.text());
                        } else {
                            $target.val($target.val() + "," + ui.draggable.text());
                        }
                    }else{
                        $target.val($target.val().replace(ui.draggable.text(),''));
                    }
                }
            });

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

How to Disable Envmap in Three.js

Incorporating an environment map into my scene has been a successful endeavor. The lines of code involved are: sceneTarget.environment = envMap; and sceneTarget.background = envMap; Now, I am seeking guidance on how to disable the environment altogether. ...

Transforming the Date into Local Time: An Instantaneous Process?

I am currently working with a Kendo UI MVC grid that contains three date columns. These dates, which do not include any time values, are stored in the database as local time rather than UTC. The columns within the grid are defined like so: col ...

Tips for personalizing the react-select module

I'm struggling with customizing the appearance of the react-select component. The official documentation lists all the style attributes that can be modified, but I'm having trouble removing the blue borders around the text and container. https:/ ...

What could be causing my image to lose responsiveness as the screen size gets smaller?

Struggling to figure out why the image is not responsive when the screen size decreases. I'm trying to create a signature block where the image adjusts in size, preventing the wrapper from stacking into two columns and keeping it as one. I've tr ...

Navigating through props provided within a setup function in Vuejs using the Composition API

I am facing an issue when trying to pass an object into a child component using props in the Composition API setup function of Vue. Instead of utilizing 'Abe', I want to run a query from firebase based on the displayName property of the user. As ...

Downloading xml data in an Angular table is a straightforward process that can be achieved

I have a table displaying a list of data. Each row includes an option to download XML data. 0{ firstName: null id: "04674" lastName: null orderId: "TEM001" productId: "TEM" receiptPeriod: "2016-02-26" ...

Using JavaScript to interact with HTML select elements on iOS devices

Although I'm not getting my hopes up, I thought I'd ask anyway. I am interested in using JavaScript to open a select element on mobile Safari for iPhone or iPad. After a thorough search on Google and Stack Overflow, it seems that many people sh ...

Looking to eliminate a significant gap of white space

I have been scouring the internet for hours, gathering a lot of information, but unfortunately, I haven't found exactly what I'm looking for. I have implemented this specific code in my WordPress website Additionally, I have integrated infinite ...

Changing the scrolling behavior of an iframe

I'm attempting to update the layout of to resemble . To achieve this, I've created a page with an iframe. However, I'm facing an issue with the iframe scroll function, as it does not match the desired design. I've experimented with t ...

Adjusting the transparency of each segment within a THREE.LineSegments object

I am following up on a question about passing a color array for segments to THREE.LineSegments, but I am looking for a solution that does not involve low-level shaders. I am not familiar with shaders at all, so I would prefer to avoid them if possible. I ...

Control the frequency of server requests within a set limit

Currently, I am utilizing the request-sync library to fetch data from a specific site's API. This is how my code looks: let req = request('GET', LINK, { 'headers': { 'Accept' ...

Arranging various JavaScript arrays

I've been working on a script using the Haversine formula, but I'm running into an issue where it always directs me to the first location in the array, no matter how many times I switch them around. Any suggestions? var locations = new Array( ...

Adding a Bearer Token to a GET request using location.href: A step-by-step guide

At the moment, I have all my ajax requests sending an Authentication token. However, I have created a service for exporting to excel and since ajax doesn't support that, I am using location.href for the get request. Is there a way to include the token ...

Determine the item in a collection of objects that contains a specific key

What is the most efficient method for locating an object by a specific key in JS when given an array of objects? Utilizing jQuery and underscoreJS is acceptable. I am simply seeking the simplest solution with minimal code. Illustration: Suppose we have a ...

What could be the reason for the excess white space at the top and bottom of the page?

Once I implemented a new div class to incorporate a background image, the top, bottom, and side menus on my page suddenly turned white. style.css #boundless{ background-image:url('http://thehypebr.com/wp-content/uploads/2010/09/boundless-sem-branc ...

At what point does javascript cease execution on a webpage following a hyperlink being tapped?

I am curious about the behavior of JavaScript code, specifically related to the usage of setTimeout(). When a user clicks on a link to navigate to another page, I wonder at what point the JavaScript code on the current page stops running and the code calle ...

"Troubleshooting the issue of null values in ASP.NET Core Razor Pages when using Ajax POST

I'm currently working on an ASP.NET (6.0) Razor Pages project where I am facing an issue with posting data using AJAX. Despite my efforts, the posted data always ends up being null. Even after going through similar questions on Stack Overflow, I have ...

How can I create a navigation bar with a search bar and a dropdown button using Material

Hello, excited to be posting my third question on SO after lurking for like 7 years. Currently, I am working with the Materialize framework and trying to design a search navigation bar with a vertical dropdown on the right side. This is how my code looks: ...

JQuery validation inspecting which submission button is activated

How can I implement validation and determine which button the user has clicked? I have a submit button with the following HTML: <form id="form-post"> <button type="submit" id="one"> ONE </button> <button type="submit" id="two ...

How to designate a default selection value using local array data source in Select2.js version 4.0?

If I am using the select2.js v4 plugin with a local array data source, how can I set the default selected value? For instance, consider the following code: var data_names = [{ id: 0, text: "Henri", }, { id: 1, text: "John", }, { id: 2, text: ...