I am trying to have a form of text inputs. All inputs will in an ordered list OL/LI structure. There will be a + symbol next to the input that when you click on it, it should create a sub-OL and make a secondary list with another input. If you click it again, it adds another input. The new inputs have + symbols as well and you can do the same thing. I only technically want it to go three deep, but I think I can figure that part out later on, but what I'm really after is how to make this happen.
I have a beginning phase of it, but it's duplicating too much of the LI and I'm not sure what's going on. This is what I have.
$('.add_sub_page').live('click',function(e){
$("<ol>").append($(this).parent().clone()).insertAfter(this);
e.preventDefault();
});
I brought this up an IRC chat room and I was provided with this solution, but it has the same issue as my first solution, so I'm not sure if it's easier to work with or not, so I will provide it as well.
$('.add_sub_page').live('click',function(e){
var ol = $("ol",this), new_ol = ol.length ? false : true;
ol = new_ol ? $("<ol>") : ol;
ol.append($(this).parent().clone());
if (new_ol) {
ol.insertAfter(this);
}
e.preventDefault();
});
And just so you can see what HTML I am working with...
<form id="sitemap" action="" method="post">
<ol>
<li class="page-title"><label>Page Name: </label><input type="text" name="pages[]" value=""><a class="add_sub_page">+</a></li>
<li class="page-title"><label>Page Name: </label><input type="text" name="pages[]" value=""><a class="add_sub_page">+</a></li>
<li class="page-title"><label>Page Name: </label><input type="text" name="pages[]" value=""><a class="add_sub_page">+</a></li>
<li class="page-title"><label>Page Name: </label><input type="text" name="pages[]" value=""><a class="add_sub_page">+</a></li>
<li class="page-title"><label>Page Name: </label><input type="text" name="pages[]" value=""><a class="add_sub_page">+</a></li>
</ol>
<p><input type="submit" name="submit_step1" value="Next Step"></p>
</form>
You can preview the issue at http://jsbin.com/everu3 -- click the same Plus (+) sign twice to see the issue.
Thanks for any help you can provide!