Manage the jQuery resize handles by controlling their behavior when clicked inside and outside of the corresponding DIV

I am dynamically creating DIV elements and using jQuery resizeable handles on them.

Is there a way to show the handles on click or hover, and then hide them when clicking outside the relevant div?

Check out this jsFiddle

$('.resizable').on('click', function() {
  $(this).addClass('focus');
});

$(document).on('click', function() {
  if (!$(e.target).closest('.resizable').length) {
    $('.resizable').removeClass('focus');
  }
});

Answer №1

To activate it, you can set it up for a click event or a hover event.

Click:

$('.resizable').resizable({
    handles: 'se'
});

////////////////////////////////////////////////////////////////////////////////////
//How can I make the following code work for dynamically created $('.resizable') DIVs?

$('.resizable').on('click', function() {
  $(this).addClass('focus');
});

$(document).on('click', function(e) {
  if (!$(e.target).closest('.resizable').length) {
    $('.resizable').removeClass('focus');
  }
});
.resizable{
    width: 100px;
    height: 100px;
    background-color: red;
    display: inline-block;
}
.resizable.focus {
    background: #f0f;
}

.resizable .ui-resizable-handle {
    background-color: black;
    display: none !important;
}
.resizable.focus .ui-resizable-handle {
    display: block !important;
}
<div id="dragDiv1" class="resizable"></div>

<div id="dragDiv2" class="resizable"></div>

<div id="dragDiv3" class="resizable"></div>

Alternatively, if you prefer hover functionality, simply replace the .resizable.focus with .resizable:hover. http://jsfiddle.net/sjchn0L8/2/

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 method for altering the choices within one selection based on the selection made in another dropdown menu

Welcome to the code snippet! <select id="type"> <option value="item1">item1</option> <option value="item2">item2</option> <option value="item3">item3</option> </select> <select id="size"> <option v ...

Interactive table created with DataTables that automatically updates the dynamic JSON data source whenever changes are made to the table

Using the Datatables plugin, I am dynamically populating a table with HTML rendered from a JSON array. However, I need the table to update the model (datasource) stored client-side whenever an edit is made. When navigating to a new page on the table, it s ...

Identifying circular interdependencies within a project

Recently, I encountered an issue with circular dependencies in my project. While I was able to resolve it, I would like to prevent this from happening again in the future. I am considering creating a plugin that can detect circular dependencies throughout ...

Do matrix filters in IE7 take into account white space sensitivity?

Utilizing CSS transformations, I am reverting to the following Matrix transform specifically for IE7 and IE8. filter:progid:DXImageTransform.Microsoft.Matrix(M11=0.0, M12=0.33, M21=-0.33, M22=0.0,sizingMethod='auto expand') This method function ...

Manipulate the content of any webpage without using external extensions by injecting your own HTML, CSS, and JavaScript code

I am currently following a tutorial on how to draw shapes on a local webpage using Canvas. I came across this helpful article on Html5 canvas paint. Everything is going smoothly, but now I am interested in replicating the drawing functions/CSS/HTML and "i ...

Eliminate parameter from URL

Here is the URL I am working with: http://my.site/?code=74e30ef2-109c-4b75-b8d6-89bdce1aa860 My goal is to redirect to this URL: http://my.site#/homepage To achieve this, I use the following code snippet: import { push } from 'react-router-redux& ...

What could have caused the lack of output from the render function?

I've been working on generating my navigation drawer from JSON data and have everything functioning using components. Now, I'm in the process of refactoring to functions for better performance and to enhance my knowledge of React and JavaScript. ...

What is the best way to select the child of the second-to-last child?

In the structure of my HTML code, I have an unordered list with a class "menu" containing several list items and corresponding anchor tags like this <ul class="menu"> <li> <a href="#1"></a> </li> <div&g ...

Several treeviews for selecting data

I need some help with a specific assignment. The back end code is all set, but I'm struggling with the UI component. The task requires two tree views, one on the left and one on the right. The left tree view will contain states with multiple children, ...

Preserving the selected options in a dynamically populated dropdown list after submitting a form using php

I am attempting to preserve form values even after submitting the form. document.getElementById('start_date').value = "<?php echo $_POST['start_date'];?>"; document.getElementById('end_date').value = "<?php echo $_P ...

Increase the dimensions of the jQuery modal confirmation box

I am working on a jQuery confirmation dialog that displays some details for the user. My goal is to have the dialog adjust its dimensions after the user clicks "YES" to show a smaller text like "Please wait...". How can I dynamically change the size of the ...

Codeigniter dropdown sending incorrect value to controller

When I choose an option from the dropdown list and click the submit button in the modal, the selected value is not being posted to the controller as expected. Below is my view code: <div class="form-group"> <div class="col-md-4"> ...

The success callback in the first call will only be triggered when a breakpoint is established

I currently have an ASP.NET MVC webpage with Bootstrap and several plugins integrated. I am attempting to implement a confirmation message using the Bootbox plugin before deleting a record, followed by reloading the page upon successful deletion. Everythi ...

Importing events from the calendar causes disarray in other data columns when sorted by date

I have a unique code that successfully imports my shared Google Calendar into a spreadsheet. In my medical office, I manage all appointments through a master Calendar. The calendar data includes start time, location, description, and title in columns B, ...

I recently edited my htaccess file to redirect my webpage, but now I'm facing issues with my CSS, images, and

Here is the htaccess code I am using: RewriteRule ^example1/([A-Za-z-]+)/?$ example2.php?name=$1 [NC] The page is loading, but the CSS and JS are not rendering correctly. Can anyone assist me in resolving this issue? ...

Using d3 or ajax to read a local file containing tab-separated values may result in a syntax error appearing in the Firefox development console

The reading operation is functioning as expected. However, I encountered a syntax error in the Firefox console while going through multiple files, which can be quite tedious. These files are annotation files formatted like (time \t value) with no head ...

Is it possible to count the number of days in between and then apply a specific class to each of those days

Here is the code snippet that I am working with: <div class="row"> <div class="test">02/12/2013</div> <div class="test">03/12/2013</div> <div class="test">04/12/2013</div> <div class="test"> ...

Initiate a series of tasks and await their completion using RxJS / Redux Observables

My app requires an initialisation action to be fired, followed by a series of other actions before triggering another action. I found a similar question on Stack Overflow However, when attempting this approach, only the initial APP_INIT action is executed ...

When the mouse leaves, the gauge chart component's size will expand

I have encountered a problem while using the react-gauge-chart library in my react project. Within the project, I have integrated a popover component using material-ui and incorporated the gauge chart component from the library within the popover modal. T ...