Solved

Retrieve values of an HTML form using JQUERY

Posted on 2016-12-01
2
12 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
Comment Utility
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 51

Accepted Solution

by:
Julian Hansen earned 500 total points
Comment Utility
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Use these top 10 tips to master the art of email signature design. Create an email signature design that will easily wow recipients, promote your brand and highlight your professionalism.
This article discusses how to create an extensible mechanism for linked drop downs.
In this tutorial viewers will learn how add a scalable full-width header using CSS3. Create a new HTML document with an internal stylesheet. Set a tiled background.:  Create a new div and name it Header. Position it with position:absolute at the top…
In this tutorial viewers will learn how to style elements, such a divs, with a "drop shadow" effect using the CSS box-shadow property Start with a normal styled element, such as a div.: In the element's style, type the box shadow property: "box-shad…

744 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now