jquery - Grabbing and storing DOM values to multidimentional array -
i building page dynamically loads 'entry's multiple fields (textarea, time_from, time_until).
i need somehow think of way jquery grab 'entry' multiple fields , store them in 2 dimensional array pass ajax server side.
what have:
<div class="entry" id="1"> <textarea class="report_actions">entry1</textarea> <input class="time_from report_actions" value="2015-05-03 17:13"> <input class="time_until report_actions" value="2015-06-18 17:13"> </div> <div class="entry" id="2"> <textarea class="report_actions">entry1</textarea> <input class="time_from report_actions" value="2015-05-03 17:13"> <input class="time_until report_actions" value="2015-06-18 17:13"> </div> i able in 1 array following:
var actions = $.makearray($('.entry').find('.report_actions').map(function(index){ return $(this).val(); })); but grabs values , makes 1-dimensional array. 2 ideas coming mind. lack of knowledge make them true therefore though i'll turn them questions.
- how can upgrade command have, nest 1 array another?
- how can split array have. i.e.([1,2,3,4,5,6]) -> ([1,2,3],[4,5,6])
edit: @tymejv did trick. calling alert(values[x][y]) can pull value want
var values = $('.entry').map(function(){ return [$(this).find('.report_actions').map(function(){ return this.value; }).get()]; }).get(); alert(values[0][2])
just use 2 .map functions:
var values = []; $(".entries").each(function() { var childvalues = $(this).children().map(function() { return this.value; }).get(); values.push(childvalues); }) should create 2d array, each 3 values each input under entries
demo: http://jsfiddle.net/ggm866u4/
edit: 2 .map calls, have wrap second .map in array, since it'll auto-flatten:
var values = $('.entry').map(function(){ return [$(this).find('.report_actions').map(function(){ return this.value; }).get()]; }).get();
Comments
Post a Comment