Jump to content

Jquery and reloading


adam84

Recommended Posts

Hey,

 

I am using JQuery, draggable and sortable on my site. The user does whatever they do and if they made a change I use ajax to update my database with the changes. I also do a page refresh every 60secs, and once the page refresh  my JQuery functions stop working. When I drop an item, I want to id to be alerted. Before the refresh the page works fine, but after the refresh nothing gets alerted. To get the page working again, I need to refresh the page mulitple times manually. Anyone ever have this issue before?

 

$( ".weekday" ).droppable({
	drop: function( event, ui ){
		alert($(ui.draggable).attr("id"));
	}
});

Link to comment
Share on other sites

This could be because of loading from cache and execution time. If javascript runs before the dom is created, you will get errors. This happens even if you put the code below the dom element because new browsers execute cached or local javascript very fast.

 

You would need ot make sure everything is loaded and use the ready function.

Link to comment
Share on other sites

I thought it might have been the cache, so I added something into the header section. Since I added that, the same issue has been happening, so I don't believe that was the problem.  I believe everything is loaded because, I can drag items around my page just like normal, but the problem is when I drop the item, I try to get the ID of the dropped item, but when I alert the ID, nothing shows up, but if I refresh the page multiple times, it finally goes.

Link to comment
Share on other sites

Try to change it to the following and see if that's solve the problem

 

<html>
<head>CSS</head>
<body>
html code

JS source files

script tag

$(document).ready(function(){
script code that require dom
});

</body>
</html>

Link to comment
Share on other sites

It still gives me an error.

 

But after some testing, I think I know what the promlem is, but I am not sure what to do to fix it.

I am using droppable, and when I drop an item, i use the 'drop' function. In that function I get the ID of the div I dropped and store it in a variable. I am also using sortable, I use the 'receive' function, in this function I parse the stored ID, because it is made up of two other ID. My error happens during the parsing of the stored ID. What I think is happening is that the droppable function is not getting called, so it is not stored that ID I need.

 

The only fix I can think of is to get the item ID in the sortable function, but I couldn't find a solution to get the value I needed. Any ideas?

Link to comment
Share on other sites

Solved,

 

In the sortable:receive function, I need to use $(ui.item).attr('id') to get the current ID of the item being 'sorted'. Then I didnt need to use the droppable function which for some reason wasn't always getting called. Nonetheless, it works and all is well, thanks

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.