views:

34

answers:

2

Hi,

I have a page that display some data. It is loaded from a database using php and mysql, I use zend framework to handle all this.

On this page I have two things that use jquery. one is a paginator and the other is a thumps up function.

the paginator works fine. It receives the data as json and applys it to the view. all the functions that I need to handle this are located in one js file. In this file I listen for clicks...

$(document).ready(function() {
$("a#next").click(getProgramms);
$("a#previous").click(getProgramms);
$("a#page").each(function() {
    $(this).click(getProgramms);
});
});

Now I have a problem with the thumps up function. It is located in another js file. Everytime the thumbs up button is clicked the script should just alert "click". actually when you click before you use the paginator a "click" appears, but when you do it after nothing happens. but the html in the dom inspector appears to be the same.

in my thumpsup.js I just have

$(document).ready(function() {
$("a.tp").click(thumpsUp);
});

function thumpsUp() {
alert("click");
}

I do not know where the problem is. maybe the js files are interferring each other!?

function thumpsUp() {
var url = window.location.hostname + '/programme/thumpsup/id/'
        + $(this).attr('page');
$.post(url, {
    "format" : "json"
}, function(data) {
    alert(data);
}, 'html');
return false;

}

A: 

You might have the Script files (which are included in your mark up) the wrong way round. That's the only solution I can think of...

I'm pretty sure you can get away with two $(document).ready()'s (even if it is frowned upon).

Neurofluxation
It looks like he is already using two $(document).ready() functions. The order might help the first time, but might not after the the paginator is used again if my guess is right.
MacAnthony
Yeah, that's what I meant.. He shouldn't use two document ready functions - but he is...
Neurofluxation
Ok, I misunderstood. I thought you were suggesting he use 2 .ready() functions.
MacAnthony
+1  A: 

I'm guessing the paginator is rewriting your elements and they are losing their click event binding. Try using live() for event binding instead:

$(document).ready(function() {
    $("a.tp").live('click',thumpsUp);
});

function thumpsUp() {
    alert("click");
}
MacAnthony
thanks that helped. Earlier I used just one document ready function. I split it up in two function to look for errors... but there is still the problem, the click is displayed now. but when I call the thumpsUp function the data that I alert is paginator data, but the url points to a view that prints just "test"...I added some code above. any idea how to solve that?
ArtWorkAD
I don't think there is enough info to answer with certainty. What is 'data' being set to? And are you saying $(this).attr('page') returns 'test'? And is 'test' correct or not?
MacAnthony
well data is the response isn't it? the alert shows html code, but since the url points to a view that just shows "test" it prints the code of another page oO
ArtWorkAD
ok, I found out that window.location.hostname seems to be wrong. defining the path manually everyting works fine
ArtWorkAD
ok, that would explain why you were getting 'data' to be the pagination results since it wasn't getting a correct response back to the ajax call, it wasn't overwriting the variable.
MacAnthony