Make the button both draggable and editable at the same time

Whenever I click on a button, it generates another button dynamically.

$('#button')
.click(function (eventClick, posX, posY) {
    var htmlData = '<div id="btn' + $.count + '" class="draggable ui-widget-content ui-draggable" ' + 'data-page="' + $.page + '" ';
    if (posX != null && posY != null) {
        htmlData += 'style="left:' + Math.abs(posX - 439) + 'px; top:' + Math.abs(posY - 124) + 'px;""';
    }

    htmlData += '><button id="editable' + $.count + '" style="width:100%; height:100%">Button</button><a href="#" class="delete" style="z-index:999"></a></div>';

    $('.demo').append(htmlData);
    $('.draggable').draggable({
        containment: "#workspace",
        scroll: false,
        cancel: false,

    })
        .resizable({
            containment: "#workspace"
        })
        .click(function () {
            if ($(this).is('.ui-draggable-dragging')) {
                return;
            }
            $(this).draggable("option", "disabled", true);
            $(this).attr('contenteditable', 'true');
        })
        .blur(function () {
            $(this).draggable('option', 'disabled', false);
            $(this).attr('contenteditable', 'false');
        });
    $('a.delete').on('click', function (e) {
        e.preventDefault();
        btnID = $(this).closest('.draggable')[0].id;
        //alert('Now deleting "'+objID+'"');
        $('#' + btnID + '').remove();
    });

    $.count++;
});

This piece of JavaScript is executed when the create button is clicked. The new button created within a div has properties such as being draggable, resizable, and deletable, with editable content. However, an issue arises while editing the text inside the button – when all the content is erased, only the div tags remain, without the button tag. Identifying the root cause of this problem has proven to be challenging.

Answer №1

It appears that the contentEditable attribute is being applied to the div element due to its 'draggable' class. This enables the deletion of all content within the DIV, including the button. To make the text within the button editable, you should apply the contentEditable attribute directly to the button element.

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

Aggregate X and Y values based on a key in a scatter plot using dc.js

Here is a glimpse of my dataset: var items = [ {name: "X", duration: 1, quantity: 2}, {name: "X", duration: 2, quantity: 1}, {name: "Y", duration: 1, quantity: 4}, {name: "X", duration: 3, quantity: 1 ...

Adjusting the margin in a flex grid using CSS

I am trying to create a grid layout using flex, where I want to have a 3x3 grid of blocks with a margin between each block. However, when I add a margin, it changes the layout to 2x5 instead. Can anyone provide guidance on how to achieve a 3x3 grid with ...

Sending a JavaScript variable to a Ruby on Rails controller for passing it to an API for further processing

My index.html file contains a Google API map implementation. <!DOCTYPE html> <html> <head> <meta name="viewport" content="initial-scale=1.0, user-scalable=no"> <meta charset="utf-8"> <style> html, body { height: ...

Display various formats in a pop-up window when different buttons are clicked

There has to be a simpler way to handle this situation. Currently, I have a modal with 4 hidden forms, one of which is active based on the "active" class. I want to show each form when different buttons like sign-up, forgot password, or sign-in are clicked ...

Is there a way to change the input type=file value?

What is the best method for customizing a file upload using input type="file" in HTML? Here is an example of what I am trying to achieve: <form enctype="multipart/form-data" action="xmlupload" method="post" name="form1" id="form1"> <input type ...

Tips for clearing all content within a React Material UI table

How can I create a reset button to clear a table filled with user input data, without having to write functions from scratch? For example, similar to using clearLayer() for clearing a leaflet map. Thank you for any suggestions! //example of dynamical ...

Retrieving the Latest State from a Custom React Hook within an Event Listener

I'm encountering a state synchronization issue in a React component involving an event handler and a custom hook. The event handler updates one state variable, and I need to access the most recent value of another state variable that is derived from a ...

PHP AJAX - data updating/editing functions insert new data instead of updating existing data

Data insert & Deletion functions are functioning properly. However, the edit feature is not updating the data as intended. Instead, it inserts a new entry into the database. The code for both INSERT and UPDATE operations is written in a single file us ...

trouble with maintaining nodejs mariadb connection

Hello, I am working with nodejs to create a rest API However, I have encountered an issue Let's take a look at the code var http = require('http'); var url = require('url'); var mariadb = require('mariadb'); http.c ...

Difficulty encountered with document.querySelectorAll retrieving paginated elements

I am currently developing a project called STEEP. Our implementation involves utilizing infinite scroll to load 8 videos at a time as the user scrolls through the page. However, we are facing an issue with the script responsible for handling video playbac ...

Choose the initial option from a dropdown menu

Is there a way to consistently choose the first item in an HTML select box by index without using val() method? ...

Encountered a JavaScript error while parsing the HTTP response

Currently, I am faced with the challenge of extracting and processing data from the www.skiddle.com API using Javascript: $.ajax({ url: 'http://www.skiddle.com/api/v1/events/search/?api_key=myapikey' + '&latitude=' + lat + ...

Utilize Google Earth Engine to specify bands when using the ee.Image.getDownloadURL() function

I am trying to specify certain bands along with a corresponding scale argument for each band in the getDownloadURL function, but I'm having trouble getting it to function correctly. var area = /* color: #d63000 */ee.Geometry.Polygon( [[[-3.199081 ...

Encountering an 'Unexpected token x in JSON at position 1' error while attempting to parse a JSON string embedded within HTML

I've been attempting to convert a hardcoded JSON string into a usable format, but I am struggling to make it work properly. I keep encountering 'Unexpected token' errors or strange outputs. The library I am using is jQuery. The data that nee ...

Conceal the div upon clicking the input field

I have a script that hides a static utility navigation bar when an input is clicked. However, this code is causing some performance issues as it runs multiple times. I'm new to JavaScript and jQuery, so I'm not sure how to optimize it. Any sugges ...

What is the process for inserting images into SQL Server using PHP?

I'm currently working on a form that allows users to upload multiple images. Despite the code working and successfully adding the images to the "images-design" folder, I am facing an issue where the names of the pictures are not being added to the da ...

Tips for displaying the ampersand symbol correctly in XSLT without any issues

When I convert all instances of & to &amp; in my XML code, the XSLT compiles successfully. Now, as I style the XML into HTML, I encounter an issue when displaying text inside a textbox that has been processed by the XSLT. Specifically, the textbox ...

Dealing with substantial ajax responses using JavaScript

Currently, I am in the process of developing a website that utilizes jQuery File Tree. However, there is an issue with the enormous size of the AJAX response from the server - 900 KB and containing approximately 70,000 'files' (which are not actu ...

Tips for updating the modal content with a dynamic approach upon clicking an image

On my HTML page, I have 3 thumbnail images. What I want to achieve is that when a user clicks on any of these images, the clicked image should be displayed in a modal window. Is there a way to dynamically pass the image name into the modal-body every time ...

Using Javascript to convert an SVG file into DOM elements

Seeking assistance with uploading an SVG file and then inspecting or parsing it to utilize the elements and values within the DOM. After extensive searches, I've only found information on parsing from the DOM itself. Is this task feasible? If so, cou ...