javascript - jQuery: How to create array with two values for each item -


i new jquery , hope can me , provide short explanation can apply similar cases in future.

i have large html page built dynamically. page contains several tables divs editable (contenteditable=true). these divs have class "editable".

now create array these divs contains id , content (text).

so far have following should create unique ids these divs incrementing number not sure on how create array this. also, out of curiosity, there term how call such arrays 2 values per item ?

my jquery:

$('#btnsave').on('click', function(){     var = 0;     $(this).closest('form').find('div.editable').each(function(){         $(this).attr('id', 'ed' + i+1);         if( ($(this).text != '') && ($(this).text != ' ') ){             $(this).addclass('edited');         }         i++;     }); });  // attempt array (perhaps wrong approach): var arredited = new array(); $('div.edited').each(function(){     arredited.push($.trim($(this).text())); }); 

many in advance, mike

i don't think need loop, instead can put inside first loop, inside if( ($(this).text() != '') && ($(this).text() != ' ') ), push object array instead of value.

var arredited = new array(); $('#btnsave').on('click', function(){     $(this).closest('form').find('div.editable').each(function(index){         //you use index when use .each function         $(this).attr('id', 'ed' + (index+1));         if( ($(this).text() != '') && ($(this).text() != ' ') ){             $(this).addclass('edited');             //instead of using loop, can put code here             arredited.push({                 id: $(this).attr('id'),                 text: $.trim($(this).text())             });             //here use object, people call array of objects         }     }); }); 

Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

spring cloud - How to configure SpringCloud Eureka instance to point to https on non standard port -

javascript - Bootstrap Popover: iOS Safari strange behaviour -