Is it possible to use data() and attr() to retrieve original css in Firefox, but face issues in Opera and Chrome?

Is there a way to reset the position of a draggable div back to its original spot after it has been dragged using jquery ui's draggable()? I attempted this:

$('#nav').draggable();

$('#nav').data({'x': $("#nav").css('left'), 'y': $("#nav").css('top')});

$("#c").click(function () {
    $('#nav').animate({'left': parseInt($("#nav").data('x')) - 15, 'top': parseInt($("#nav").data('y')) - 14}, {duration : 500});
});

This method seems to work in older versions of Firefox but not in the latest Opera and Chrome browsers. I also tried replacing data() with attr(), without success.

Anyone know how to make this work consistently across different browsers?

Edit: You can see this code in action here: http://jsfiddle.net/MVCA6/

Answer №1

When using chrome, I have observed a result by using the following code:

console.log($("#nav").data('x'));

The result is auto;

However, in Mozilla, it returns

447px

This difference may be why it is not functioning correctly in chrome. But if you specify the left and top properties in the #nav CSS as

left:447px; top:352px

It will work properly. VIEW DEMO HERE

For further details, you can refer to THIS LINK

Answer №2

An issue arises here because jQuery UI Draggable relies on the top and left properties for moving elements, while you are positioning the red boxes using bottom and right instead;

If you add console.log($('#nav').data()) to the click event, you will notice that your x and y properties are both set to auto.

To resolve this, you must adjust your CSS to position the red box using top and left.

For example: http://jsfiddle.net/MVCA6/1/

On a side note: Proper code indentation can greatly aid in debugging issues.

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

Filtering with XML parsing

My XML file has the following structure - <?xml version="1.0" encoding="UTF-8"?> <prj:Flow xmlns:prj="url1" xmlns:com="url2" xmlns:ns2="url3" xmlns:con="url4" xmlns:ns0="url5" xmlns:ns1="url6" xmlns:ns3="url7"> <prj:str> &l ...

Adding a fresh data point to Highcharts

Can the highchart line graph be updated every minute without starting from scratch? I want it to add new data points to the existing line, similar to the example shown in this jsfiddle. $(function () { $.getJSON('https://www.highcharts.com/sample ...

Which is quicker when utilizing jQuery selectors: selecting by .classname or div.classname?

Which is quicker: using $(".classname"). or adding the tag to search for as well? $("div.classname") In my opinion, using just the classname would be faster because jQuery can simply loop through all classnames directly, whereas in the second example i ...

Why is the toggle list not functioning properly following the JSON data load?

I attempted to create a color system management, but as a beginner, I find it quite challenging! My issue is: When I load my HTML page, everything works fine. However, when I click on the "li" element to load JSON, all my toggle elements stop working!!! ...

Customizing CSS according to specific URLs

I have two different domains - one ending with .nl and the other ending with .be. For instance, domain.nl and domain.be. Both domains share a similar overall style, but I want certain elements to have distinct styling depending on whether it is the .nl o ...

Can JavaScript be used to modify the headers of an HTTP request?

Can JavaScript be used to modify or establish HTTP request headers? ...

What exactly is the relationship between the DOM and the Window object?

Could you please provide me with a visual depiction of the DOM and window within a browser? I am curious about their placement in the interface. ...

Having issues when dynamically adding options to a multiselect dropdown

I'm working on dynamically adding options to a multiselect using AJAX: <select size='2' name="CraftCode" id=@ccrf class="form-control js-select manualentrydd" ></select> $.ajax({ type: "GET", url: "GetCraftCodes", data: ...

Using Aurelia CLI: Incorporating npm package containing image assets into your Aurelia CLI application

Currently, I am attempting to incorporate lightslider (jquery slider library) from NPM into my Aurelia CLI 0.23.0 project. In order to achieve this, I have updated the dependencies in aurelia.json as follows: { "name": "lightslider", ...

Trouble with setting HTML table width

<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title></title> </head> <body> <table border="1" width="100%"> <tr> <td>bbbbbbbbbbbbbbbbbbbbbbbbbbb ...

Deactivate radio buttons when the table value is greater than or equal to one

On my webpage, there are radio buttons for 'Yes' and 'No'. When 'Yes' is selected, a hidden <div> appears where users can fill in fields. Upon clicking the 'Add' button, the information is displayed in a table. ...

Creating interactive input fields within a table

I have a dynamic table consisting of rows that can be sorted, with inputs allowing users to add more rows as needed. Now, I want to group these rows into sets. Here's the concept: <tr> <td><input type="text" name="set[0][name]" value ...

The Bootstrap Row emerges from the shadow of the margin

Recently, I started diving into the world of Bootstrap. While playing around with some code to create rows resembling a table, I noticed an issue. When I loaded the code in my browser, the margins of the rows appeared off-screen. Take a look at this simple ...

Issue encountered when attempting to populate input values in a form after retrieving data from the backend

Currently, I am developing a form that will submit user data to the backend using AJAX upon hitting the submit button. After fetching additional values from the backend through AJAX, I need to redirect back to the frontend. My goal is to retrieve these val ...

Applying ngClass to a row in an Angular material table

Is there a way I can utilize the select-option in an Angular select element to alter the css-class of a specific row within an Angular Material table? I have successfully implemented my selection functionality, where I am able to mark a planet as "selecte ...

Transform straightforward JavaScript code into jQuery

I'm relatively new to the world of JS and jQuery, and I have a simple working JS function that I would like to convert into jQuery. By doing this, I believe it will enhance my understanding of the inner workings of jQuery (although I already have a ba ...

Implementing data updates in Ruby on Rails through AJAX or jQuery

Within a text field input lies the value of a database attribute (ref). Upon focusing on the field, a border appears and disappears upon clicking out. My dilemma is that I wish for the data within the text field to be saved in the database without the nee ...

Concealing Social Security Numbers using AngularJS

I am currently working on masking SSN numbers using AngularJS. Expected Outcome: Before applying mask (onFocus) After applying mask (onBlur) Users are only allowed to enter numbers, and the SSN formatting is handled by filters. Below is a sample of the ...

There seems to be a limitation in JS/JQuery where it is unable to append HTML that spans

I am encountering an issue with retrieving data in a div element. It seems that the function provided does not work correctly for files larger than a single line of code or even possibly a string. What can be done to resolve this? <ul class="dropdo ...

What steps can I take to ensure my website is optimized for a seamless viewing experience on all mobile devices

Could you please check out the link on your smartphone and let me know if it looks a bit off to you? I have centered everything and the website looks fine on desktop, but on my iPhone 5S, it doesn't seem to display properly. Is there a way to make it ...