0

I have the following code:

$( "#check-button" ).on( "click", function() {
    $("#button_cart").click();
    $('.checkout-button').click();
});

I have this jQuery click event. What I want to do is, on clicking the check-button, the first line inside the function is updating the cart (which reloads the page) and then the second line inside the function is the checkout button, which takes me to the next (checkout) page.

But here only the first event is firing. Is this because the page is reloaded on "button-cart" clicking? How do I solve it?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Diffy
  • 2,339
  • 3
  • 25
  • 47
  • Calling click only fires the event handlers attached if any, not the default browser's behaviour – Stphane Apr 06 '14 at 18:56
  • The page reload is by default written in the shopping cart platform that i am using @foobar – Diffy Apr 06 '14 at 18:57
  • if the page is reloaded, then wont i be able to access the second click @f00bar ? – Diffy Apr 06 '14 at 18:59
  • _"the first line inside the function is updating the cart ( which reloads the page)"_ when the page reloads your script stops executing,because as it no longer exists – Patrick Evans Apr 06 '14 at 18:59
  • so how do i solve this problem @PatrickEvans – Diffy Apr 06 '14 at 19:00
  • i think it will be better to update cart with ajax call instead of reloading page. if page reloads functions after " $("#button_cart").click(); " will never be called. – krozero Apr 06 '14 at 19:00
  • No. the function to reload is inbuilt written in cs-cart( shopping cart platform) – Diffy Apr 06 '14 at 19:01

2 Answers2

1

That's correct. The second click actually could be working but in some kind of limbo between the click and the load and you wont see it.

The solution is to "handle" the reload event, I put it between "" because this event can't be handled ( as far as I know) but you can make some hacks.

First, why is reloading the page? Are you adding new content?

In this case just call the click in the content added with a load handler like $(document).ready();

nach
  • 349
  • 1
  • 3
  • 10
  • reloading is inbuilt written in cs-cart platform. I cant change it – Diffy Apr 06 '14 at 19:08
  • You can add the second click in the cs-cart then. Can you share the code? – nach Apr 06 '14 at 19:15
  • Then check any changes in the page that you could use to fire the second click, that's the only way. You can look or classes added for example. I recommend you to use a cms with fully accessible code, unless you wont be able to customize much. – nach Apr 06 '14 at 19:26
  • 1
    If you can code php, you can use a session cookie and store the reload event in a variable. The execute the click if the variable is > 0 – nach Apr 06 '14 at 19:28
  • Yes even i can do it using js cookie or php cookie. – Diffy Apr 06 '14 at 19:29
0

Here is how i did it: Using localstorage.

Just saved the value of someVariable on check-button click ( along with button-cart click) and on page reload i checked if the value is set. If it is set, i unset it and clicked the next button. Here is the link

This was really great help from SO. Thank u SO

Community
  • 1
  • 1
Diffy
  • 2,339
  • 3
  • 25
  • 47