javascript - get all data from one list item jquery mvc 4 -
i want click on 1 row , id posting db , name dsiplay user.
with code have click on corresponding <li>
(data-id or data-name) value. how both <li>
? hidden-fields, maybe?
$(".ita").on("click", "li", function () { var div = $("#addeditems"); var itemtoadd = $(this).attr("data-id"); var name = $(this).attr("data-name"); alert(itemtoadd + name); var itemtoadd = ("<li class = " + itemtoadd + ">"+ name +"</li>"); alert(itemtoadd); $(itemtoadd).appendto(div); // div.html(itemtoadd); });
view
@foreach (var item in model) { <ul> <li data-id = "@item.id"> @html.displayfor(modelitem => item.id) </li> <li data-name="@item.item_name"> @html.displayfor(modelitem => item.item_name) </li> <li> @html.displayfor(modelitem => item.item_description) </li> </ul> }
try:
$(".ita").on("click", "li", function () { var div = $("#addeditems"); //get nearest parent var parent = $(this).closest("ul"); //from parent, access both li var itemtoadd = parent.find("[data-id]").attr("data-id"); var name = parent.find("[data-name]").attr("data-name"); alert(itemtoadd + name); var itemtoadd = ("<li class = " + itemtoadd + ">"+ name +"</li>"); alert(itemtoadd); $(itemtoadd).appendto(div); // div.html(itemtoadd); });
Comments
Post a Comment