HTML:
<section id="intro">
<div id="ih" contenteditable="true">Header</div>
<div id="ish" contenteditable="true">Subheader</div>
<a id="ibtn" contenteditable="true">Test</a>
</section>
<section id="articles">
<div id="ah" contenteditable="true">Header</div>
<div id="ash" contenteditable="true">Subheader</div>
<a id="btn-1" contenteditable="true">Button</a>
<a id="btn-2" contenteditable="true">Button</a>
<a id="btn-3" contenteditable="true">Button</a>
</section>
jQuery:
$(document).ready(function() {
$('#edit-button').click(function() {
var dataObj = [];
var jsonObj = [];
$('section').each(function() {
var section = $(this).attr('id');
if( $(this).find('[contenteditable]').length != 0 ) {
$('[contenteditable=true]').each(function() {
var id = $(this).attr('id');
dataObj.push('\"' + id + '\":\"' + $(this).html() + '\"');
});
jsonObj.push('{\"' + section + '\":{' + dataObj + '}}');
dataObj.length = 0;
} else {
return;
}
});
alert(jsonObj);
});
});
Goal:
Create an JSON array(?) which contains the section name followed by a set of elementID+html if any of the sections children contains the attribute contenteditable and that attribute is set to true.
Currently it's including it's not ending the jsonObj as it should, it includes the second section into that array as well and for the second section includes the first sections data.
I've tried to empty the dataObj array using dataObj.length = 0; without success.
What am I doing wrong?
Final goal:
Create a inline editor connected to a Laravel 5.1 back-end