0

Let's say I have this code:

<div id="content">
</div>

and I use a simple ajax request to fill the content with data which work when I click some button

    $.get("page?num=" + pageNumber, function(data){
                $("div#content").html(data);
});

one time the data will be

<div id="Data1">
</div>

and other time it will be

<div id="Data2">
</div>

for each id I have differnet jquery functions. for exampe:

$('#Data1').click(function(){});

some of the functions are similar and some are different.

My question - if I click the button I load all the scripts I need. when I click the other button I need to load the scripts again for the new content, but what happens to the old functions that was relevant to the previous content? Each time I click I load new scripts without deleting the last ones.

How do I delete/manage my scripts correctly?

thanks, Alon

2
  • Can you tell us where you're calling: $('#Data1').click(function(){}); and presumably, $('#Data2').click(function(){}); Commented Dec 15, 2011 at 7:49
  • when I think of it - all of my scripts are the same for each load. I was wondering how I solve it in my case, and how I will solve it if I would have different scripts... Commented Dec 15, 2011 at 8:41

4 Answers 4

1

I am not sure if i understood your question. are you expecting something like

$("div#content").html(""); // deleting existing data
$("div#content").html(new_data); //loading new data
Sign up to request clarification or add additional context in comments.

2 Comments

no - I am expecting $("div#content").html(somedata); or $("div#content").html(someOtherdata);
yeah.. then the above will work. the 1st line will remove all the existing data. second line will load new data
1

jQuery binds events to DOM objects (divs) in this case when they are called. Which means it looks for an element with the given ID and then binds it. Since Edit1 and Edit2 does not exists when the script is run, they are not bound.

You can try to bind them each time you change their ID.

function rebind() {
    $('#content').click(function(){});
    $('#Data1').click(function(){});
    $('#Data2').click(function(){});
}

Call this whenever you load new content in the div.

3 Comments

I can't use live because I am bound to jQuery 1.4.3. Anyway, what is the difference between using rebind() and not using it - either way if it doesn't find the element it doesn't attach the function.and what is the difference between bind() and rebind()?
@Alon: You didn't check the docs right? It says it's been there since version 1.3. I think if you are using version 1.4.3, you should be good with it.
I misunderstood what they meant there - thanks for the notice!
1

Gurung is right about the event handler, but as of jQuery 1.7, the .live() method is deprecated. Use .on() to attach event handlers instead.

(I could not post this information as a comment, so it had to be a new answer, sorry aboyt that).

Comments

0

May should just use the .live() function.

Description: Attach an event handler for all elements which match the current selector, now and in the future.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.