Clicking an element triggers Sortable to bind an event

There is a draggable element in my code, which is represented by a div

<div class="question-constructor multiple-choice-problem">
<label for="textbox" class="question-label"> #. Edit this to define question: </label>
   <input type="radio" name="test" id="option-1-1" class="question-radio" style="float:left;">
   <input type="radio" name="test" id="option-1-2" class="question-radio" style="float:left;">
   <input type="radio" name="test" id="option-1-3" class="question-radio" style="float:left;">
   <input type="radio" name="test" id="option-1-4" class="question-radio" style="float:left;">
</div>

These draggable divs are then dropped into the sortable area using the following destination div:

<div id="preview" ></div>

Below is the JavaScript used for sorting:

var sortableIn = 0;
$('#preview').sortable({
     receive: function(event, ui) {             
        sortableIn = 1;
        $(ui.item).addClass('editable');
     },
     placeholder: 'ph',
     over: function(event, ui) { sortableIn = 1; },
     out: function(event, ui) { sortableIn = 0; },
     start: function( event, ui ) {
                $(ui.helper).addClass('on-sort');
     },
     beforeStop: function (event, ui) {
            if (sortableIn == 0) { 
                ui.item.remove(); 
            }
     },
     stop: function( event, ui ) {
            $(this).find('.on-sort').removeClass("on-sort");
     }
});

My aim is to create an onclick event specifically for the sortable items. I attempted to achieve this with the following code:

$(function() {
    $(document).on('click', '#preview ', function() {
        console.log('clickable');
    });
});

However, clicking on the sortable items triggers the event for everything instead of just the item clicked. What I want to do is change the text of the label within the specific sortable div that was clicked.

For instance:

  1. I dragged the .question-constructor div three times into the #preview div (resulting in three items).
  2. When I click on the 2nd item in the sortable list, I wish to modify the text label on that particular item only.

How can I achieve this?

If there is any confusion regarding my question, feel free to leave a comment and I will provide further clarification.

Answer №1

One option is to utilize the on-click event specifically for the item that was clicked:

$(".item-clicked").on("click", function (e) {
    $(e.target).text('I have been updated');
});

Check out this demo: http://jsfiddle.net/aVcxR/

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

What is the best way to apply styling to an image that is contained within the document.write function?

I'm looking to customize the design of this cat image, but I'm struggling to locate where to incorporate the div class. It's likely a basic step, but as a beginner in JavaScript, I'm hoping that someone can assist me. document.write(&ap ...

Unlocking the npm package event emitter on the client side in Meteor

Having some trouble with a seemingly basic issue. I came across an NPM package called LOG-WATCHER (used as an example) which keeps an eye on a specific log file on a client's local file system. This package emits events such as 'START', &apo ...

Ways to connect a click event to a dynamically generated child element with the help of jQuery?

I am aware that similar questions have been asked elsewhere, but as someone new to jQuery, I am still struggling to attach a click listener to an a element within a dynamically appended ul.li.a structure in the DOM. Below is an example of how the structure ...

Discovering the names of parameters when using decorators

I am in the process of creating a decorator to verify the validity of certain parameters. Below is the method I am working on, getUsers(offset, limit, orderBy, sort){ .... } I want to ensure that the value of orderBy is either 'createdAt' or & ...

What is the best way to change the value of a variable in JavaScript?

I am completely new to JavaScript and have never worked with it before. As part of a school assignment involving a Python server using HTML templates, I need to create a button in a .tpl file that will increment a variable by 1 each time it is clicked: &l ...

Adding HTML components to an image with adjustable dimensions

A graphic designer has provided us with an image featuring three selection boxes. I implemented the necessary HTML elements and CSS to display three overlapped selection boxes on top of the image, using pixel values for positioning. However, the original ...

Utilize open-source tools for website design

Can anyone suggest an open source toolset for WYSIWYG HTML/CSS design? What tools have you found most helpful in your projects? ...

Guide to assigning unique identifiers to all elements within an array using JavaScript

I have an array of objects with numeric keys that correspond to specific data values. I am attempting to restructure this object in a way that includes an 'id' field for each entry. Here is the original object: [ { "1": "data1", "5": "d ...

Issues with BrowserHistory in react-router causing malfunction

My URL structure is currently storing history in hash syntax like this: Ex: http://localhost:3000/#/work?_k=otstr8 I am attempting to switch to using browserHistory from react-router so that it displays as: http://localhost:3000/#/work Below is my r ...

Ways to eliminate unused classes from JQuery UI

We have successfully implemented JQuery UI library for enhancing our interface. However, since we no longer need to support outdated browsers like IE6, classes such as .ui-corner-all, .ui-corner-top, .ui-corner-left, .ui-corner-tl are unnecessary and clu ...

Send the component template and functions when triggering an expanded view in a material dialog

While striving to adhere to DRY coding principles, I've encountered a dilemma involving a particular use case. The task at hand is to display an expanded view of a component within a dialog box. This component presents JSON records in a paginated list ...

Retrieve the JSON data along with its corresponding keys

My goal is to showcase JSON data that looks like this: var jsonData = ({ "respond": 1, "paging": { "stillmore": 0, "perpage": "10", "callpage": "1", "next": 0, "previous": 0, ...

Loop through the input template using ng-repeat with various data types

I have been working on developing a user interface for adding and modifying information in a database. To manage the database, I am utilizing breeze, displaying data using angular grid, and incorporating a ui-bootstrap modal dialog for user interaction. ...

Having trouble getting the Basic Drag and Drop JavaScript code to function correctly?

Apologies, my English skills are not the best! I attempted to implement a basic JavaScript, HTML, and CSS drag-and-drop feature but it doesn't seem to be working correctly. Can someone please take a look at the website to see what is going wrong? ...

How to return the same value as the input value in Vue before the action is completed

When creating a component and module for functionality X while using Vuex for state management, the code initially works fine. However, after the first insertion, the Getter function consistently returns the previous input value before the action is commit ...

Leveraging nested objects within React state

A React child cannot be an object. If you intended to render a collection of children, utilize an array Encountering the error mentioned above has left me puzzled regarding its restrictions. Below is the code where I am facing this issue. It includes an o ...

Implementing a Preloader in a React Web App

I am looking to implement a preloader in my React application because it takes a long time to load. I want the preloader to automatically render until all the contents of my application are fully ready to be loaded. Is it possible to achieve this? I could ...

Is there a way for me to choose all the classNames that conclude with a specific word within the MUI sx property?

I am currently working with MUI and I have a need to modify certain properties that are prefixed with random IDs. How can I target, for instance, the first one using: '& endsWith(MuiAccordionDetails-root)' I wish to achieve this because the ...

Prevent pushing values to an array in JavaScript by disabling the Array

I am facing a challenge with a sealed object that has an array member, and I want to restrict direct pushes. var myModule = (function () { "use strict"; var a = (function () { var _b = {}, _c = _c = "", _d = []; ...

Image gallery floating

The image in the last position of this gallery is supposed to float left, but instead it appears centered. Can you spot the error in the code? To view the complete CSS code, click here. If you want to see the full HTML code, check it out here. This is t ...