0

I want to add an array to this working example of my project. You can see the functionality I'm going for if you answer the question in the field and click enter to see it update.

JQuery:

   var $input = $("#input");
   var questions = ["What's your name?", "Are you married?", "What's your wife's name?", "Do you have children?", "How many children do you have?"];
   $(function () {
       var scntDiv = $('#namess');
       var i = $('#namess p').size() + 1;

       $('#addScnt').live('click', function () {
           $('<p><label for="names"><input type="text" name="names_' + i + '" id="names_' + i + '"size="30" value="' + $input.val() + '"></label><a href="#" id="remScnt">  Remove</a></p>').appendTo(scntDiv);
           i++;
           $input.val("Are you married?");
           return false;
       });

       $('#remScnt').live('click', function () {
           if (i > 1) {
               $(this).parents('p').remove();
               i--;
           }
           return false;
       });
   });

HTML:

<input type="text" name="input" id="input" size="30" value="What is your name?">
<a href="#" id="addScnt">Enter</a>

<div id="namess">
<div>
    <label for="names"></label>
</div>
</div>

http://jsfiddle.net/miketrujillo/nV2tA

I included an array in there just as an example of what I'd like to see in the text field. I've tried adding the questions var into the value for names but it's not working correctly and is breaking the cloning to the box.

krp
  • 25
  • 3
  • Are you aware that `.live()` has been deprecated and even removed from jQuery? If you're using v1.7 or later, you would use the delegated form of `.on()` to replace it. See [here](http://stackoverflow.com/questions/8752321/jquery-live-vs-on-method-for-adding-a-click-event-after-loading-dynamic-ht/8752376#8752376) for details on how to use `.on()` instead. If you're still on an old version of jQuery, you can use `.delegate()` instead of `.live()`. – jfriend00 Mar 10 '14 at 22:08
  • Also ID's need to be unique, and you're appending the same ID'd elements each time. – dsgriffin Mar 10 '14 at 22:09

0 Answers0