Utilize jQuery resizable handlers to attach and resize a div element

Having trouble attaching the handlers to resize $('oWrapper_'+num). The resizing isn't working as expected. Could it be because the handlers are linked to the wrong div?

The goal is to attach the handlers to $('oWrapper_'+num) and successfully resize using those handlers.

    num++
    var cloudWrap = $('<div />', { id: 'cloudWrap_'+num}),
        outerWrap = $('<div />').appendTo(cloudWrap)

        outerWrap.append(
            $('<div />', { class: 'tf', id: 'oWrapper_'+num, style: 'white-space:pre-line; font-size: 2vw;' }),
            $('<div />', { class: 'ui-resizable-handle ui-resizable-ne hndl' }),
            $('<div />', { class: 'ui-resizable-handle ui-resizable-se hndl' }),
            $('<div />', { class: 'ui-resizable-handle ui-resizable-sw hndl' }),
            $('<div />', { class: 'ui-resizable-handle ui-resizable-nw hndl' })
        );


        jQuery('#oWrapper_'+num).resizable({
          handles: {
            'ne': '.ui-resizable-ne',
            'se': '.ui-resizable-se',
            'sw': '.ui-resizable-sw',
            'nw': '.ui-resizable-nw'
          },
          aspectRatio: 16 / 9,
        });

EDIT:

outerWrap.resizable({
              handles: {
                'ne': '.ui-resizable-ne',
                'se': '.ui-resizable-se',
                'sw': '.ui-resizable-sw',
                'nw': '.ui-resizable-nw'
              },
              aspectRatio: 16 / 9,
          create: function(event, ui) {
                initDiagonal = getContentDiagonal();
                initFontSize = parseInt($("#oWrapper_"+num).css("font-size"));
            },
            resize: function(e, ui) {
                var newDiagonal = getContentDiagonal();
                var ratio = newDiagonal / initDiagonal;

                $("#oWrapper_"+num).css("font-size", (initFontSize/100) + (ratio / (initFontSize/1000))  + "vw");
            }
        });

function getContentDiagonal() {
    var contentWidth = $("#oWrapper_"+CretCount).width();
    var contentHeight = $("#oWrapper_"+CretCount).height();
    return contentWidth * contentWidth + contentHeight * contentHeight;
}

Answer №1

The issue lies with the id selector jQuery('#oWrapper_'+num). Since this is called before the new element is added to the DOM, it fails to find any elements and consequently does not initialize the resizable handler.

To resolve this, you can use the variable cloudWrap which points to the newly created element for widget initialization.

num++;
var cloudWrap = $('<div />', { id: 'cloudWrap_'+num}),
    outerWrap = $('<div />').appendTo(cloudWrap)

outerWrap.append(
    $('<div />', { class: 'tf', id: 'oWrapper_'+num, style: 'white-space:pre-line; font-size: 2vw;' }),
    $('<div />', { class: 'ui-resizable-handle ui-resizable-ne hndl' }),
    $('<div />', { class: 'ui-resizable-handle ui-resizable-se hndl' }),
    $('<div />', { class: 'ui-resizable-handle ui-resizable-sw hndl' }),
    $('<div />', { class: 'ui-resizable-handle ui-resizable-nw hndl' })
);


cloudWrap.resizable({
    handles: {
        'ne': '.ui-resizable-ne',
        'se': '.ui-resizable-se',
        'sw': '.ui-resizable-sw',
        'nw': '.ui-resizable-nw'
    },
    aspectRatio: 16 / 9,
});

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 are the steps to utilize chrome.tabs.onUpdated.addListener?

I am currently working on a Chrome extension where I need to display an alert() showing the page URL whenever the user switches tabs or enters a new URL in a tab. However, my current implementation is not functioning as expected: chrome.tabs.onUpdated.ad ...

Having trouble retrieving JSON data due to a CORS or callback error

I am attempting to retrieve JSON data from a REST API, but when making an AJAX request with the dataType parameter set as jsonp, I encounter an error stating that the jQuery 'callback was not called'. The error message reads: Error: Status: pa ...

Is there a way to automatically highlight an input field upon page load?

Is there a way for certain websites to automatically highlight an input field upon entering a page? For instance, YouTube's login page automatically highlights the Username field as soon as you land on it. Even this site, particularly on the Ask Ques ...

Exploring the concept of passing 'this' as a parameter in JavaScript

<button type="button" aria-haspopup="dialog" aria-controls="b" onclick="openLayer($('#b'))"> button</button> <div role="dialog" id="b"><"button type="button">close<"/button></div> function openLayer(target){ ...

disable readonly attribute on several fields by clicking button with jquery

I am struggling with selecting all the input fields of a row in my student details table at once when clicking the edit button. The fields are readonly and can be edited by clicking the edit button. Below is the HTML code snippet: <table class="table ...

Personalizing the WordPress Admin dashboard

Looking to personalize the appearance of the WordPress dashboard left-side menu. My goal is to eliminate the need for hovering over sub-menus to display them, and instead have them visible in the menu at all times. For instance, rather than having to hover ...

What is the best method for aligning buttons in a row with all the other buttons?

I have been attempting to display two buttons, id="strength" and id="minion", when a certain button, id="expandButton", is clicked. I want these two buttons to be positioned on either side of the button that triggers their cre ...

What is the best way to clear the contents of the `<Form>` field after submitting, while still maintaining it as a server component?

Utilizing a server component for the Search form, I encountered an issue when trying to empty the input field upon clicking the cancel button. Despite importing a <SearchReset/> client component, I struggled to reset or clear the form. SearchForm.ts ...

What are the steps to design a basic circle in CSS3 that allows for easy movement and resizing of the background image?

I attempted to achieve a specific effect with my code snippet: .mask { width: 200px; height: 200px; border-radius: 50%; -webkit-border-radius: 50%; -moz-border-radius: 50%; background: url(http://lapen ...

Is there a way to incorporate arguments into my discord.js commands?

Hey there! I'm looking to enhance my Discord commands by adding arguments, such as !ban {username}. Any tips or guidance on the best approach for this would be amazing! const Bot = new Discord.Bot({ intents: ["GUILD_MESSAGES", "GUIL ...

CSS animations for loading page content

Currently, I am incorporating animations into my project using HTML5 and CSS3, and the progress has been smooth. I have been able to achieve effects such as: #someDivId { position: absolute; background:rgba(255,0,0,0.75); transition: all 0.7s ...

Seeking to develop a pair of functions for submitting data via my website's form

I need to pass form data to both Docusign and Zapier with just one button click using JavaScript. When the Submit button is pressed, I want the data to be sent to Zapier without opening a success page and then redirect to the Docusign page with all the in ...

Searching for a specific value within a list that has been fetched from a database using AngularJs can be achieved by simply clicking on the search button

Seeking assistance on a search functionality issue. I am able to filter search results from a list retrieved from the database and displayed on an HTML page, but facing difficulty in getting complete search results from the controller. When clicking the se ...

Setting up an i18n project in AngularJS

I just embarked on my angularjs journey yesterday with little to no prior knowledge about it. My initial goal is to centralize all the labels for my UI in a file (to facilitate swapping them out for i18n). As far as I know, this can be achieved by importi ...

What makes Firefox so much speedier than Chrome when it comes to loading hefty documents?

Currently, I am facing an issue with slow loading times for custom HTML build reports on Google Chrome. It takes a significantly longer time to load compared to Firefox. Approximately 5 seconds on Firefox versus 110 seconds on Google Chrome (measured usi ...

Tips for ensuring your webpage stays current with regular updates through JavaScript

This webpage acts as a dynamic newsboard, constantly updated with the latest data from my Database through an API, all without requiring a page refresh. While the timer solution seemed simple, I am seeking a more resource-efficient approach. I have resear ...

What is the process to turn my function into a promise?

Can someone help me "promisify" my custom function located in a different directory? Here is the code snippet: // app.js // include database var mongo = require('./mongo'); var promise = require('bluebird'); var u = require('./ut ...

Is there a way to shift an entire row to the right?

I need assistance with aligning a row of columns, each containing a button, to the center of the page. <div> <div class="container-fluid" id="home"> <h1>Welcome to Daniel's Portafolio</h1> <div class="row"> <div ...

What is the necessity of utilizing getStaticPaths() alongside getStaticProps()?

When working with dynamic routes, such as [id].js, the static pages generated using npm run build will result in an [id].html page. Any route containing /something will display "Hello World". However, when we dynamically generate content on the page by ut ...

What could be the reason for the image not appearing from the database?

I am having trouble displaying both the image and title from my database. Currently, only the title is showing up. Why isn't the image displaying? Below is the code I am using: @foreach($posts as $post) <tr> < ...