Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5959083
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T18:38:09+00:00 2026-05-22T18:38:09+00:00

The goal is, when I focus on the last text line, another one appears

  • 0

The goal is, when I focus on the last text line, another one appears below it. Then disable this at a certain number of lines.
I can’t seem to figure out what is wrong here:

$(document).ready(function() {
    lineCount = 0;
    $("form#qc").delegate("input:text:last-child", "focus", newTextLine);
});

function newTextLine() {
    newLine = ("<div id='ansInput{0}'>Answer {0}: <input type='text' name='ans1' /></div><!--ans1-->").format(lineCount);
    currentDiv = ("#ansInput{0}").format(lineCount);
    $(currentDiv).after(newLine);
    lineCount = lineCount++;
}

this is the HTML page:

<form id="qc" name="qc">
<h2>Create New Question!</h2>
<div id="ansInput0">Question: <input type="text" name="Question" /></div><!--question-->
<input type="submit" value="Submit" />
</form>

I get a very very weired result: each time two lines appear and all have index of 0 and all respond the event handler that is suppose to work only for the last one…
Any ideas what’s wrong with the code?

JSfiddle

Also any advice on how to make the code smarter is welcome!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T18:38:10+00:00Added an answer on May 22, 2026 at 6:38 pm

    As said, there are some problems in your code.
    I’d try it like this:
    Cloning the last div, changing the id and clearing the value of the input:

    $(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());
        });
    });
    

    http://jsfiddle.net/7enNF/1/


    • input:text:last-child selects inputs which are the last child element in their parent. so it would select all the inputs, because each of them is the only (therefore last) child in the ansInput div
    • #qc>div:last-of-type>input selects the input in the last div. i used last-of-type because last-child wouldn’t match the div, because the submit button is the last child of the form
    • the end is necessary because i select the div, then i select the input with “find(‘input’)” to clear the value. if i’d leave it that way it would only insert the input (not the div). So the end() to get back from the input to the div again so that the div will be inserted.

    i hope this wasn’t too confusing! 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Goal Pass images generated by one process efficiently and at very high speed to
Goal I need to alter a number of almost identical triggers on a number
goal : I want to place a text after the submit button using hook_form_alter.
My goal is to set the focus to a specific inputText. As there seems
On this page: www.incfilms.net/services, my goal is to make that Advertising and Consulting picture
Some thoughts are that the ELEMENT_ID.focus() is inside divs that are hidden at certain
My goal is to apply onFocus = if(this.blur)this.blur(); to all the area sections under
The following jQuery's goal is to load images from a server, and then control
GOAL: I'm trying to apply a Like button to my blog - this is
Goal Java client for Yahoo's HotJobs Resumé Search REST API . Background I'm used

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.