J
J Y
Hi,
I'm writing a Rails app that uses some Javascript to allow users to
click a button to add more input fields to the page. I have the code
working, but it's very unelegant, with the HTML forms packed into the
Javascript code.
I don't want to do any server-side stuff for something this simple.
I'd like to be able to put most of the HTML somewhere in a hidden div,
but can't think of a way to make that work, since I need to increment
the index of the "name" field for each new input field.
What is the best practice for doing this? Surely not the way I have
done it.
A snippet of my code is below.
function add_new_link_fields(new_count){
new_fields = '<li><span class="newlink_label">New link:</span><span
class="field_label">URL:</span><span class="r"><input id="site_url"
type="text" name="site[' + new_count + ']"/></span></li><li><span
class="field_label">Title:</span><span class="r"><span
class="r1"><input id="site_title" type="text" name="site[' + new_count
+ '][title]"/></span><span class="r2">Tags: <input id="site_tag_list"
type="text" name="site[' + new_count + '][tag_list]"/></span></span></
li><li><span class="field_label">Description:</span><span
class="r"><input type="text" id="site_description" name="site[' +
new_count + '][description]"><span class="controls" id="controls' +
new_count + '"></span></span></li>';
saved_values = Form.getElements('ajax_submit_form')
$('new_link_fields').innerHTML += new_fields
if (new_count > 1) {
// Restore saved values
for (i in saved_values) {
e = saved_values
if (e.value && e.type != 'submit') {
$('ajax_submit_form').elements[e.name].value = e.value
}
}
old_count = new_count - 1;
$('controls' + old_count).innerHTML = "";
$('in_place_create_submit_button').value = "Add links";
}
$('controls' + new_count).innerHTML = $('controls_source').innerHTML;
}
I'm writing a Rails app that uses some Javascript to allow users to
click a button to add more input fields to the page. I have the code
working, but it's very unelegant, with the HTML forms packed into the
Javascript code.
I don't want to do any server-side stuff for something this simple.
I'd like to be able to put most of the HTML somewhere in a hidden div,
but can't think of a way to make that work, since I need to increment
the index of the "name" field for each new input field.
What is the best practice for doing this? Surely not the way I have
done it.
A snippet of my code is below.
function add_new_link_fields(new_count){
new_fields = '<li><span class="newlink_label">New link:</span><span
class="field_label">URL:</span><span class="r"><input id="site_url"
type="text" name="site[' + new_count + ']"/></span></li><li><span
class="field_label">Title:</span><span class="r"><span
class="r1"><input id="site_title" type="text" name="site[' + new_count
+ '][title]"/></span><span class="r2">Tags: <input id="site_tag_list"
type="text" name="site[' + new_count + '][tag_list]"/></span></span></
li><li><span class="field_label">Description:</span><span
class="r"><input type="text" id="site_description" name="site[' +
new_count + '][description]"><span class="controls" id="controls' +
new_count + '"></span></span></li>';
saved_values = Form.getElements('ajax_submit_form')
$('new_link_fields').innerHTML += new_fields
if (new_count > 1) {
// Restore saved values
for (i in saved_values) {
e = saved_values
if (e.value && e.type != 'submit') {
$('ajax_submit_form').elements[e.name].value = e.value
}
}
old_count = new_count - 1;
$('controls' + old_count).innerHTML = "";
$('in_place_create_submit_button').value = "Add links";
}
$('controls' + new_count).innerHTML = $('controls_source').innerHTML;
}