Finding the dynamic width of a div using JavaScript

I have two divs named demo1 and demo2. I want the width of demo2 to automatically adjust when I drag demo1 left to right or right to left.

<div class="wrapper">
              <div class="demo"></div>
              <div class="demo2"></div>
</div> 
<STYLE>
.demo {
    float: left; 
    width: 50%;
    height:100%;
    background-color: red;
}
.demo2 { 
    float: left;
    background-color: black;
    width: 50%;
    height:100%;
}</STYLE>
<script type="text/javascript">
         (function(){

      maxWidth=$('.wrapper').width();
      $('.demo').resizable(function(e){

        var x = e.pageX;
        alert(x);

        $(this).width(x);
        $('.demo2').width(maxWidth-x);


    });
})()
 </script>

However, the width of demo2 does not adjust automatically as desired when dragging demo1. Can anyone help me identify what is wrong in my code?

Answer №1

It seems there may have been a misunderstanding of the syntax here. To achieve your desired functionality, you can utilize the resize event like so:

(function () {
    var maxWidth = $('.container').width();
    $('.box').resizable({
        resize: function (e, ui) {
            var x = e.pageX;
            $(this).width(x);
            $('.box2').width(maxWidth - x);;
        }
    });
})();

Check out the demo here: http://jsfiddle.net/abc123xyz/1/

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

Displaying the appropriate DIV element based on the value entered by the user

I am encountering some difficulties... I have an <input> field. Depending on the input, one of the various <div> elements should be displayed. For now, it's just a text (e.g. "Your plan contains less than 1500 kcal!"), but later, the div ...

"UIWebView experiences a crash when a long press gesture is performed on a textarea element

Looking to implement a custom keyboard for UIWebView that is entirely based on HTML/JS/CSS for compatibility across multiple devices. To achieve this, I included a notification as shown below: [[NSNotificationCenter defaultCenter] addObserver:self selecto ...

Failure of Angular to execute HTTP calls asynchronously

I am feeling a bit perplexed about how and when to use the .then and/or .success functions. Here is a function example: $scope.handleData = function(option){ if(option == 1){ link = firstLink/; }else{ link = secondLink/; } ...

Detecting changes in checkbox states

In my current scenario, I have a section of the screen that can be shown or hidden depending on whether a checkbox is checked. The user can change the state of the checkbox manually or programmatically. The challenge lies in detecting this change and upda ...

Error: Reference 'ref' is undefined in the 'no-undef' context. I am interested in experimenting with loading images from Firebase storage

While working with React, I encountered an issue when trying to fetch an image URL from Firebase Storage and display the image. The error 'ref' is not defined (no-undef) occurred. https://firebase.google.com/docs/storage/web/create-reference Th ...

What is the best way to store the dom in cache to ensure that the page remains unchanged when navigating back using the back button?

When adding models to my JavaScript application's model collection using AJAX calls, I encounter an issue where if I click on a model and go to the next page, all the loaded models disappear when I hit the back button. What is the most effective way t ...

What is the best way to add a component into a MAP of other components in a React application?

I have a project where I am implementing a MAP function to iterate through an array of objects and present the data using a special Card component. However, I want to include another component called Banner after every second Card element from the loop, an ...

Tips for transferring PHP variable from a drop down menu

Hello, I am currently working on creating a dropdown menu using the <select> tag. My goal is to have it so that when someone clicks on one of the options in the dropdown, a new window opens. Additionally, I want the PHP variable from the main page to ...

Having trouble with SVG background image not displaying properly and positioning correctly?

I need help fitting an svg as a background into an element that is 80% of the screen width. Here is the desired final result: The svg effect only works correctly when I reduce the width, but then it breaks in two. I want the svg to appear at 80% width a ...

Ensuring the HTML5 video poster matches the dimensions of the video content

Is there a way to adjust the HTML5 video poster so it perfectly fits the dimensions of the video itself? Check out this JSFiddle demonstrating the issue: http://jsfiddle.net/zPacg/7/ Here's the code snippet: HTML: <video controls width="100%" h ...

Is there a way to determine if a React functional component has been displayed in the code?

Currently, I am working on implementing logging to track the time it takes for a functional component in React to render. My main challenge is determining when the rendering of the component is complete and visible to the user on the front end. I believe t ...

Acquire a personalized WCF entity using Json representation

[SOLVED] I recently created a test project to help me understand the process of working with WCF objects and Json. If you have any valuable resources or tutorials on this topic, please share them with me. My current challenge involves retrieving a custom ...

Ways to conceal the header and footer on specific React pages

I currently have my header and footer wrapping the content on all pages of my website. However, I am looking to hide the header and footer on specific pages like the customer and admin dashboard. Can anyone suggest the best approach to achieve this? cons ...

Are you ready to create a Modal Factory?

For a while now, I have been utilizing modals in various front-end frameworks to communicate with users in my applications. Typically, the process involves defining the modal's html and then rendering it through a click event. As my apps continue to ...

troubles encountered in implementing JavaScript to toggle the display of content upon clicking a link

Here is the code below. Upon page load, the form data will be hidden. When clicking on the comment link, it will toggle the visibility of the form content. Clicking on the comment link again will show the hidden content once more. What is desired is to ha ...

Removing duplicate data from Table TD and TR elements using Jquery

I am working on an HTML table and I need to remove duplicate values in each <tr> and <td> data cell. Here is a snippet of my code: <table width="100%" id="l2table"> <thead><tr> ...

Employing the validateAll() function in conjunction with a personalized v-select component within the scope

One of my recent scenarios involves scoping a form in order to validate it using the Vee-Validate method shown below. validateTRForm: function (scope) { this.$validator.validateAll(scope).then((result) => { if (result) { } ...

Automatically selecting checkboxes from an array using ReactJS

Hello there, I am a beginner working with react and I could really use some help with the issue below. Can you assist me? I am trying to figure out how to populate or check/uncheck checkboxes based on an Array's result. Specifically, I want to have ...

Find the average value of an array containing objects

Imagine I have an array of objects like this: const BookDetails = [ { bookName: 'Harry Pottar', readingTime: 10663 }, { bookName: 'Harry Pottar', readingTime: 10986 }, { bookName: 'kaptura Tech', readingTime: 7034 } ] I ...

Tips for sequentially arranging and rearranging an array of numbers, even when duplicates are present

Encountered a perplexing issue that has me scratching my head in an attempt to visualize a solution. Currently, I am working with an array of objects that appears as follows: let approvers = [{order:1, dueDate: someDate},{order:2, dueDate: someDate}, ...