SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast guioconnor's Avatar
    Join Date
    Oct 2007
    Posts
    96
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Collect form data with Javascript

    Hi,

    I am building an e-commerce and I am using some Ajax on it so each product in a listing has a button to add the product to the cart without reloading the page.

    I don't know how to collect the hidden input fields in each form so they can be passed to POST.

    Each product on the list has a form like this

    HTML Code:
    <form class="options" action="shop/cart/add/" method="post" onsubmit="addToCart(); return false;">
      <input type="hidden" size="2" name="quantity" value="1" class="priced quantity" /> 
      <input type="hidden" name="productname" class="productname" value="product-2" />
      <button type="submit">Add to cart</button>
    </form>
    How do I collect quantity and productname to pass to the addToCart() function?
    Guilherme Zühlke O'Connor
    www.z-oc.com

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    The form element has an elements array that references all of the form elements

    So from the addToCart() function, you can use this.form.elements to get them all.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Enthusiast guioconnor's Avatar
    Join Date
    Oct 2007
    Posts
    96
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ah, great. Many thanks.

    While I waited fot an answer I have found a way to do it, but this one is much more elegant, I'll switch to your solution.

    Thanks again.
    Guilherme Zühlke O'Connor
    www.z-oc.com


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •