Solved

Retrieve values of an HTML form using JQUERY

Posted on 2016-12-01
2
65 Views
Last Modified: 2016-12-02
I have the below code.

How do I retrieve the value of class snipcart-details after the "add to cart" button has been clicked ?.

I keep getting the value of the first product even if I click on the second or third product.
        <script>

			$(document).ready(function() {

			    $(".button").click(function() {
					alert("Value: " + $("#item_name").val());
			    });
			});
			

        </script>  

Open in new window




                              
<div class="col-md-3 w3ls_w3l_banner_left">
						<div class="hover14 column">
						<div class="agile_top_brand_left_grid w3l_agile_top_brand_left_grid">
							<div class="agile_top_brand_left_grid_pos">
								<img src="images/offer.png" alt=" " class="img-responsive" />
							</div>
							<div class="agile_top_brand_left_grid1">
								<figure>
									<div class="snipcart-item block">
										<div class="snipcart-thumb">
											<a href="single.html"><img src="images/58.png" alt=" " class="img-responsive" /></a>
											<p>junior pet food (90 gm)</p>
											<h4>$5.00 <span>$6.00</span></h4>
										</div>
										<div class="snipcart-details">
											<form action="#" method="post">
												<fieldset>
													<input type="input" id="cmd" value="_cart" />
													<input type="input" id="add" value="1" />
													<input type="input" id="business" value=" " />
													<input type="input" id="item_name" value="junior pet food" />
													<input type="input" id="amount" value="5.00" />
													<input type="input" id="discount_amount" value="1.00" />
													<input type="input" id="currency_code" value="USD" />
													<input type="input" id="return" value=" " />
													<input type="input" id="cancel_return" value=" " />
													<input type="submit" name="submit" value="Add to cart" class="button" />
												</fieldset>
											</form>
										</div>
									</div>
								</figure>
							</div>
						</div>
						</div>
					</div>
					<div class="col-md-3 w3ls_w3l_banner_left w3ls_w3l_banner_left_asd">
						<div class="hover14 column">
						<div class="agile_top_brand_left_grid w3l_agile_top_brand_left_grid">
							<div class="agile_top_brand_left_grid_pos">
								<img src="images/offer.png" alt=" " class="img-responsive" />
							</div>
							<div class="agile_top_brand_left_grid1">
								<figure>
									<div class="snipcart-item block">
										<div class="snipcart-thumb">
											<a href="single.html"><img src="images/4.png" alt=" " class="img-responsive" /></a>
											<p>dogs food - junior (4 Kg)</p>
											<h4>$9.00 <span>$11.00</span></h4>
										</div>
										<div class="snipcart-details">
											<form action="#" method="post">
												<fieldset>
													<input type="input" id="cmd" value="_cart" />
													<input type="input" id="add" value="1" />
													<input type="input" id="business" value=" " />
													<input type="input" id="item_name" value="dogs food - junior" />
													<input type="input" id="amount" value="9.00" />
													<input type="input" id="discount_amount" value="1.00" />
													<input type="input" id="currency_code" value="USD" />
													<input type="input" id="return" value=" " />
													<input type="input" id="cancel_return" value=" " />
													<input type="submit" name="submit" value="Add to cart" class="button" />
												</fieldset>
											</form>
										</div>
									</div>
								</figure>
							</div>
						</div>
						</div>
					</div>
					<div class="col-md-3 w3ls_w3l_banner_left">
						<div class="hover14 column">
						<div class="agile_top_brand_left_grid w3l_agile_top_brand_left_grid">
							<div class="agile_top_brand_left_grid_pos">
								<img src="images/offer.png" alt=" " class="img-responsive" />
							</div>
							<div class="agile_top_brand_left_grid1">
								<figure>
									<div class="snipcart-item block">
										<div class="snipcart-thumb">
											<a href="single.html"><img src="images/59.png" alt=" " class="img-responsive" /></a>
											<p>gravy food for dogs (20 kg)</p>
											<h4>$15.00 <span>$18.00</span></h4>
										</div>
										<div class="snipcart-details">
											<form action="#" method="post">
												<fieldset>
													<input type="input" id="cmd" value="_cart" />
													<input type="input" id="add" value="1" />
													<input type="input" id="business" value=" " />
													<input type="input" id="item_name" value="gravy food for dogs" />
													<input type="input" id="amount" value="15.00" />
													<input type="input" id="discount_amount" value="1.00" />
													<input type="input" id="currency_code" value="USD" />
													<input type="input" id="return" value=" " />
													<input type="input" id="cancel_return" value=" " />
													<input type="submit" name="submit" value="Add to cart" class="button" />
												</fieldset>
											</form>
										</div>
									</div>
								</figure>
							</div>
						</div>
						</div>
					</div>

Open in new window

0
Comment
Question by:Errol Farro
2 Comments
 
LVL 82

Expert Comment

by:leakim971
ID: 41909514
you should have a unique ID on a page : https://www.w3.org/TR/WCAG-TECHS/H93.html
anyway here how to do it : https://jsfiddle.net/ypwezcar/2/

			$(document).ready(function() {
			    $(".button").click(function(evt) {
			               evt.preventDefault(); // this is to stop posting the form, remove it if needed
			                var $myForm = $(this).closest("form");
			                var $myItem = $("input[id='item_name']", $myForm);
					alert("Value: " + $myItem.val());
			    });
			});

Open in new window

0
 
LVL 56

Accepted Solution

by:
Julian Hansen earned 500 total points
ID: 41909801
Your problem is that you have multiple forms with elements with the same ID.

This is not legal - id's must be unique. When you do a select on ID number multiple are going to be found but because ID's must be unique it will take the the first one. Consider this code
<html>
<body>
<div id="fred">fred 1</div>
<div id="fred">fred 2</div>
<script src="http://code.jquery.com/jquery.js"></script>
<script>
$(function() {
  $('#fred').each(function(i,e) {
    alert(e.innerHTML);
  });
});
</script>
</body>
</html>

Open in new window

This will alert "Fred 1"
Now consider this code
<html>
<body>
<div class="fred">fred 1</div>
<div class="fred">fred 2</div>
<script src="http://code.jquery.com/jquery.js"></script>
<script>
$(function() {
  $('.fred').each(function(i,e) {
    alert(e.innerHTML);
  });
});
</script>
</body>
</html>

Open in new window

This will alert Fred 1 and then Fred 2 - because classes can be duplicated - when you select on a class you get all matching elements.

While the above solution will work it is essentially just putting a plaster over the problem.

Here is how I would do your page.
1. Change your id="" to name="" for each form - this can be done with a search and replace
search: input" id=" and replace with input" name="

2. Add this jQuery
<script>
$(function() {
  $('form').submit(function(e) {
    // Prevent default form submit
    e.preventDefault();
	// get all the data for the input controls on the clicked form
    var data = $(this).serialize();
	// show them in the console
    console.log(data);
  });
});
</script>

Open in new window


Working sample here
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
JavaScript Loan Calculator Error 3 34
Change how page is organized 3 35
Html date format 9 20
html form layout 4 34
This article describes how to create custom column layout styles for Bootstrap. The article uses 5 columns to illustrate the concept, but the principle can be extended to any number of columns.
Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
In this tutorial viewers will learn how to style a corner ribbon overlay for an image using CSS Create a new class by typing ".Ribbon":  Define the class' "display:" as "inline-block": Define its "position:" as "relative": Define its "overflow:" as …
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question