Solved

Retrieve values of an HTML form using JQUERY

Posted on 2016-12-01
2
84 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 57

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
Finding original email is quite difficult due to their duplicates. From this article, you will come to know why multiple duplicates of same emails appear and how to delete duplicate emails from Outlook securely and instantly while vital emails remai…
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…
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

724 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