1

I have a foreach loop that outputs a form with content for each array entry as per bellow.

$cartOutput .= '<td><form action="cart.php" method="post">
        <input name="quantity" type="text" value="' . $each_item['quantity'] . '" size="1" maxlength="2" id="quantity" />
        <input name="adjustBtn' . $item_id . '" type="submit" value="change" id="adjustBtn" hidden="true" />
        <input name="item_to_adjust" type="hidden" value="' . $item_id . '" />
        </form></td>';

As you can see the submit button has a unique assigned name for depending on the item.

In my jquery file i have the bellow.

$('#quantity').change(function(){
$("[name^=adjustBtn]").closest("form").submit();

});

This works on the first form on the page but any additional form does not auto submit on change, i believe the name^=adjustBtn entry is grabbing all of the different buttons but im just not sure how to tell it which one to submit etc.

1
  • 2
    IDs are unique to the page, you can't have more than one element with the same ID. Commented Sep 18, 2012 at 11:15

1 Answer 1

1

You seem to need

$('[name="quantity"]').change(function(){
   $(this).closest("form").submit();
});

Your id doesn't seem needed. And beware that you can't have more than one element with a given id (problem with "quantity" and "adjustBtn" in your code).

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks i have now changed the unique part of the name to be the same for the ID's and updated as per your example and works great. Thanks.

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.