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 :)

3
  • possible duplicate of javascript enable input on double click Commented Apr 29, 2014 at 10:55
  • does the loaded content use cookies/sessions? Commented Apr 29, 2014 at 10:58
  • Any online link where this behaviour can be checked? Commented Apr 29, 2014 at 11:01

1 Answer 1

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);
})
Sign up to request clarification or add additional context in comments.

3 Comments

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
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) :)
Not using inline script is always more 'elegant' ;) Now why OP's code wasn't working is still mysterious for me

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.