javascript - Get dynamic elements from page HTML? -
i new front end development have quite bit of end dev experience. i've tried few different approaches javascript below , of things tried.
i have following code shows products. in n each loop product list.
at bottom of page have add button.
i need data-id of each product productid, price price , value of qty input field can send through cart.
i can't values , have tried few different things.
<div class="col-md-2"> <div class="panel panel-primary"> <div class="panel-heading"> <h3 class="panel-title">@html.displayfor(modelitem => product.code)</h3> </div> <div class="panel-body"> <div class="text-center" id="user_image"> <img src="../../images/chickenbreast.jpg" class="img-thumbnail" /> </div> <br /> <div class="panel-body form-group-separated"> <div class="form-group"> <label class="control-label">short description:</label> <p>@html.displayfor(modelitem => product.description)</p> </div> <div class="form-group"> <label class="control-label">long description:</label> <p>@html.displayfor(modelitem => product.longdescription)</p> </div> </div> <div class="panel-footer"> <form class="form-inline" role="form"> <div class="form-group"> <input id="qty" data-id="@product.id" price="@product.price" type="text" class="product-control" placeholder="qty" /> </div> <button id="addbutton" class="btn btn-primary pull-right">add cart</button> </form> </div> </div> </div> </div>
here javascript, have tried few different things nothing seems give me required data
<script type="text/javascript" src="../../scripts/js/plugins/jquery/jquery.min.js"></script> <script type="text/javascript"> $("#addbutton").click(function (form) { var producttoupdate = $("input").parent().find("data-id"); var counttoupdate = $('input[id="qty"]', form).val(); var productprice = $(this).parent().find('price').val(); if (producttoupdate != '') { // perform ajax post $.post("~/cart/getcartitems", { "productid": producttoupdate, "qty": counttoupdate, "price": productprice }, function (data) { //check not process callback data when server error occurs //if (data.itemcount != -1) { // $('#cart-total').text(data.carttotal); //} }); } }); </script>
the selector data-id
never match. suggesting data-id
element type (example) <data-id ...></data-id>
try using find('input[data-id]')
instead.
https://api.jquery.com/category/selectors/attribute-selectors/
https://api.jquery.com/has-attribute-selector/
description: selects elements have specified attribute, value.
example documentation:
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>attributehas demo</title> <script src="https://code.jquery.com/jquery-1.10.2.js"></script> </head> <body> <div>no id</div> <div id="hey">with id</div> <div id="there">has id</div> <div>nope</div> <script> // using .one() handler executed @ once // per element per event type $( "div[id]" ).one( "click", function() { var idstring = $( ).text() + " = " + $( ).attr( "id" ); $( ).text( idstring ); }); </script> </body> </html>
// using .one() handler executed @ once // per element per event type $("div[id]").one("click", function() { var idstring = $(this).text() + " = " + $(this).attr("id"); $(this).text(idstring); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script> <!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>attributehas demo</title> <script src="https://code.jquery.com/jquery-1.10.2.js"></script> </head> <body> <div>no id</div> <div id="hey">with id</div> <div id="there">has id</div> <div>nope</div> <script> </script> </body> </html>
Comments
Post a Comment