How to put a proper jQuery selector under if condition so that it work for all the cases? -
using jquery each function, parsing div data called myordersdiv
as parsing myordersdiv
, creating array , storing data .
the issue facing that
if myordersdiv
has got 1 elmenet under working fine , o/p
[{"name":"choclate"}]
but in case if myordersdiv
has got 2 elmenets under , o/p
[{"name":"chocolatevanila "}]
(its mixing both of them )
the fiddle first case
the fiddle second case
right jquery selector is
$.each($('#myordersdiv > ul'), function(i, elem) { name = $(elem).find("label").text(); if (name != 'undefined') { //creating product array products.push({ 'name': name }); } });
could please ??
try this:-
var name; var products = []; $('#myordersdiv > ul').find('ul').each(function(){ name = $(this).find("label").text(); if (name != 'undefined') { //creating product array products.push({ 'name': name }); } }); var orderjson = json.stringify(products); console.log(orderjson);
Comments
Post a Comment