0

I'm fairly new to this web-programming thing, and I'm having some trouble with an onclick event. I don't even know if using "onclick()" is the best thing to do, but it has been working so far for me.

At this moment, I have a page with a div in which I load another page. This content varies depending on hash changed when I select options from a toolbar, using this piece of js

function loadcontent(toload){
   $('#browsediv').load("content/addimagecontent.php?"+toload);
}

Every js function is called from the main page, not the content one.

Now, my problem is that, in the loaded content, I have several pages of results, and I have a div with the word Next printed into it, and an onclick event that should make the page change its page attribute:

echo "<div onClick='loadcontent(\"page=".$nextpage."\")'>Next</div>";

I also have the same thing to lead you to the previous page.

Once I go to the page, I see everything as should, but if I click either on "Next" or "Previous", it doesn't do anything the first time. Any subsequent times I click on any of those, it works perfectly, even if the first thing I click is Next and then I click Previous or viceversa.

I've been looking around but no-one seems to have answered anything that adjusts to my issue, if someone has, please forgive me, as English is not my mother tongue and I sometimes don't know the best way to look for something. Thanks for reading :)

Iskalla
  • 381
  • 3
  • 17

1 Answers1

2

Instead of adding an onclick, add an id attribute. Then with jquery you can do something like this:

<div id="yourDiv">Next</div>

$("#yourDiv").click(function() {
    loadcontent(toload)
})

I'm not quite sure if this is "legal" but you can add the $nextPage variable as an attribute too.

<div id="yourDiv" data-page="<?php echo $nextPage;?>">Next</div>

Then you would use the following

$("#yourDiv").click(function() {
    var page = $(this).attr('data-page');
    loadcontent(page);
})
Nick
  • 2,862
  • 5
  • 34
  • 68
  • 1
    `data-page` would be valid, then get it using: `var page = $(this).data('page');` or ya, you can use too: `$(this).attr('data-page');`. That's said, i'm not sure how this would fix OP's issue – A. Wolff Apr 29 '14 at 11:02
  • we don't know if it fixes OP's issue because there is no way to test the code he has. This is just a "workaround" to rule out the odd-looking code he has written (not saying my code is ever elegant) :) – Nick Apr 29 '14 at 11:05
  • Not using inline script is always more 'elegant' ;) Now why OP's code wasn't working is still mysterious for me – A. Wolff Apr 29 '14 at 11:07