A
adam
I'm currently coding a CMS system for a site which includes the
feature to create multiple sections inside a page. To add each of
these new sections I'm using DOM with AJAX to save, but I've got a
pretty div for each section which needs to be created on the click of
a button. Here's a sample of the code, I won't post it all because
it's quite big...
function addSection() {
// Get number of current sections
var sections = 0;
var el_sections = document.getElementById('sections');
for (i=0; i<el_sections.getElementsByTagName('li').length; i++) {
if (el_sections.getElementsByTagName('li').parentNode ==
el_sections) sections++;
}
new_section = sections + 1;
// Create List Item
var section_li = document.createElement('li');
section_li.setAttribute('id', 'section_' + new_section);
// Create section_header DIV
var header_div = document.createElement('div');
header_div.className='section_header';
// Create Title Input
var title_input = document.createElement('input');
var title_input_id = 'section_'+new_section+'_title';
title_input.setAttribute('type', 'text');
title_input.setAttribute('size', '50');
title_input.setAttribute('name', 'section['+new_section+']
[section_title]');
title_input.setAttribute('id', title_input_id);
// Create Label
var title_label = document.createElement('label');
title_label.setAttribute('for', title_input_id);
title_label_text = document.createTextNode('Section Title:');
title_label.appendChild(title_label_text);
header_div.appendChild(title_label);
header_div.appendChild(title_input);
.........
section_li.appendChild(header_div);
el_sections.appendChild(section_li);
}
to create something along the lines of:
<li id="section_1">
<div id="header_1" class="section_header">
<label for="section_1_title">Section Title:</label>
<input type="text" name="section[1][section_title]"
id="section_1_title" size="50" />
</div>
<div class="section_options">
<ul>
<li>
<label for="section_1_position">Position:</label>
<select name="section[1][section_position]"
id="section_1_position">
<option value="1" selected>1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
</li>
<li><a href="javascript:;"
onClick="javascript:showHideDetails('1')" id="shlink_1">hide</a></li>
<li><a href="javascript:;"
onClick="javascript:deleteSection('1')">delete</a></li>
</ul>
</div>
<div id="details_1" class="section_content">
<textarea name="section[1][htmlelement][htmlelement_content]"
cols="60" rows="10" ></textarea>
<br />
<small>
<input type="checkbox" name="section[1][htmlelement][parse]"
id="section_1_parse"> <label for="section_1_parse">Parse Code (for
Dynamic Content)</label><br />
<input type="checkbox" name="section[1][htmlelement][hideheader]"
id="section_1_hideheader"> <label for="section_1_hideheader">Hide
Header</label>
</small>
</div>
</li>
I'm pretty new to this DOM lark, I've just pieced everything together
from tutorials across the internet, so I'm wondering, is there a more
efficient way of doing this or anything anyone can suggest?
Thanks in advance
feature to create multiple sections inside a page. To add each of
these new sections I'm using DOM with AJAX to save, but I've got a
pretty div for each section which needs to be created on the click of
a button. Here's a sample of the code, I won't post it all because
it's quite big...
function addSection() {
// Get number of current sections
var sections = 0;
var el_sections = document.getElementById('sections');
for (i=0; i<el_sections.getElementsByTagName('li').length; i++) {
if (el_sections.getElementsByTagName('li').parentNode ==
el_sections) sections++;
}
new_section = sections + 1;
// Create List Item
var section_li = document.createElement('li');
section_li.setAttribute('id', 'section_' + new_section);
// Create section_header DIV
var header_div = document.createElement('div');
header_div.className='section_header';
// Create Title Input
var title_input = document.createElement('input');
var title_input_id = 'section_'+new_section+'_title';
title_input.setAttribute('type', 'text');
title_input.setAttribute('size', '50');
title_input.setAttribute('name', 'section['+new_section+']
[section_title]');
title_input.setAttribute('id', title_input_id);
// Create Label
var title_label = document.createElement('label');
title_label.setAttribute('for', title_input_id);
title_label_text = document.createTextNode('Section Title:');
title_label.appendChild(title_label_text);
header_div.appendChild(title_label);
header_div.appendChild(title_input);
.........
section_li.appendChild(header_div);
el_sections.appendChild(section_li);
}
to create something along the lines of:
<li id="section_1">
<div id="header_1" class="section_header">
<label for="section_1_title">Section Title:</label>
<input type="text" name="section[1][section_title]"
id="section_1_title" size="50" />
</div>
<div class="section_options">
<ul>
<li>
<label for="section_1_position">Position:</label>
<select name="section[1][section_position]"
id="section_1_position">
<option value="1" selected>1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
</li>
<li><a href="javascript:;"
onClick="javascript:showHideDetails('1')" id="shlink_1">hide</a></li>
<li><a href="javascript:;"
onClick="javascript:deleteSection('1')">delete</a></li>
</ul>
</div>
<div id="details_1" class="section_content">
<textarea name="section[1][htmlelement][htmlelement_content]"
cols="60" rows="10" ></textarea>
<br />
<small>
<input type="checkbox" name="section[1][htmlelement][parse]"
id="section_1_parse"> <label for="section_1_parse">Parse Code (for
Dynamic Content)</label><br />
<input type="checkbox" name="section[1][htmlelement][hideheader]"
id="section_1_hideheader"> <label for="section_1_hideheader">Hide
Header</label>
</small>
</div>
</li>
I'm pretty new to this DOM lark, I've just pieced everything together
from tutorials across the internet, so I'm wondering, is there a more
efficient way of doing this or anything anyone can suggest?
Thanks in advance