1

I just want to get this jQuery to work. When I do this it writes to the console just fine:

$('body').on('submit', 'form[action="/cart"]', function(event){
  console.log("I don't think this is getting here");
});

But when I do this it doesn't write to the console:

$('body').on('change', 'form[action="/cart"]', function(event){
  console.log("I don't think this is getting here");
});

my test website anyone can see is at http://test-4658.myshopify.com this script is on the cart. You can add and update stuff in the cart to verify.

EDIT: Some other ideas I've tried, with no luck:

$('form[action="/cart"] :input').click(function() {
  if($(this).closest('form[action="/cart"]').data('changed')) {
  console.log("I don't think this is getting here");
  }
});

And:

$('body').on('change', 'form[action="/cart"] :input', function(event){
  console.log("I don't think this is getting here");
});

Where did I go astray?

ToddT
  • 3,084
  • 4
  • 39
  • 83
  • 1
    I strongly suspect that the form element does not have a change event. You would have to change the selector to detect change on any input/select/textarea child of the form maybe. – Vanquished Wombat Dec 30 '16 at 19:11
  • 2
    Form change event does work, see example: https://jsfiddle.net/q6rzgzq4/ -- must be something else – Will P. Dec 30 '16 at 19:11
  • `change` event relies on the `value` of an element which `form`s don't have. Try monitoring for `change` on one of the `form`'s elements/inputs. – Sean Mishra Dec 30 '16 at 19:12
  • makes sense.. let me see. but I thought that by using the `form` selector it selected all the child elements in the form? But maybe not.. – ToddT Dec 30 '16 at 19:14
  • Well, I was surprised that forms had a change event too - but your jsfiddle does work... Even when I put in your form selector: https://jsfiddle.net/q6rzgzq4/2/ – nibnut Dec 30 '16 at 19:17
  • Note that while capturing the `change` event on the `form` works (as per Will P's fiddle) it is not the `form` that's raising the event. It's relying on the event bubbling up through the DOM from the `input`, in the same way that a delegated event handler would. OP, make sure there's no other elements which are trapping that event and call `stopPropagation()` or `return false;` – Rory McCrossan Dec 30 '16 at 19:18
  • @ToddT, first - it's impossible to add items to the cart in your link without login (and probably register first). Second - it will be much easier to help if you create a working example (demo) in jsfiddle/snippet to show the problem. – Dekel Dec 30 '16 at 19:18
  • @Dekel and everyone else, sorry about that, turned off the login necessary for the cart add – ToddT Dec 30 '16 at 19:20
  • Doesn't work. Please check with incognito (or logout and check before update that "everything works") – Dekel Dec 30 '16 at 19:21
  • 1
    This might be a duplicate: http://stackoverflow.com/questions/3025396/how-do-you-handle-a-form-change-in-jquery#3025505 – LearnMoreCoding.com Dec 30 '16 at 19:22
  • No I don't think so, I've tried that answer and its not working for me, I've updated the question with the other things I've tried. – ToddT Dec 30 '16 at 19:40
  • Without fixing the login problem it's impossible to help – Dekel Dec 30 '16 at 19:44
  • Why event delegation - use normal event binding and think does element has such event. I don't like using delegation without reason. – Maciej Sikora Dec 30 '16 at 20:07
  • It ended up being a timing issue. The cart had not rendered before the script was run. I found another way! Thank you everyone – ToddT Dec 30 '16 at 21:22

0 Answers0