The goal of the code is to create a new input line each time the last line is focused, until a certain amount of lines. I was offered this solution which works great:
$(function() {
$("#qc>div:last-of-type>input").live('focus', function() {
$(this).parent().after($(this).parent().clone().attr('id', 'ansInput' + $('#qc>div').length).find('input').val('').end());
});
});
but I also want to change each time the text before the line, so clone
won't cut it
I modified it to write a pre-ready HTML line, but it doesn't work:
function questionsForm() {
$("#qc>div:last-of-type>input").live('focus', function() {
lineNum = $('#qc>div').length
newLine = ("<div id='ansInput{0}'>Answer {0}: <input type='text' name='ans{0}' /></div><!--ans1-->").format(lineNum);
if ($('#qc>div').length <= 4) {
$(this).parent().after(newLine);
}
});
}
(the .format
method is predefined)
I would like to understand why my code isn't working, and how to chamge it, but not completely different solutions (unless of course my code has some fundamental errors..)