Advertisement
Guest User

Untitled

a guest
Sep 21st, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 1.79 KB | None | 0 0
  1.  
  2.  
  3.   <script>
  4. $(document).ready(function(){
  5.    $(".add-answer").click(function(){ //bind a click event
  6.        addAnswer($(this));
  7.    });
  8. });
  9.  
  10. function addAnswer($anchor){
  11.   var questionId = $anchor.siblings("textarea").attr("id");
  12.   var answerIndex = $anchor.siblings(":text").size() + 1;
  13.   var answerId = questionId + "-answer" + answerIndex;
  14.   var $lastAnswer = $anchor.siblings(":text:last");
  15.   $lastAnswer.clone().attr("id", answerId).attr("name", answerId).after($lastAnswer);
  16. }  
  17.  
  18. function addQuestion(){
  19.   var currentQuestion = $(".questionContainer").size(); //Number of questionContainers
  20.   var $newElement = $('#questionContainer0').clone().attr('id','questionContainer'+currentQuestion);
  21.   $newElement.children('#question0'). attr('id','question'+currentQuestion).attr('name','question'+currentQuestion);
  22.   //Update the answerContainer div id.
  23.   $newElement.children('#answerContainer0') .attr('id','answerContainer'+currentQuestion);
  24.   //Update the first answer id and name
  25.   var answerId = 'question'+currentQuestion+'-answer1';
  26.   $newElement.children('#question0-answer1').attr('id',answerId).attr('name',answerId);
  27.   $newElement.appendTo('#questionArea');
  28. }
  29.   </script>
  30.   </head>
  31.  
  32.   <form method="post" action="test.cfm">
  33.   <div id="questionArea">
  34.     <div id="questionContainer0" class="questionContainer">
  35.         <textarea id="question0" name="question0"></textarea>
  36.         <div id="answerContainer0">
  37.         <input type="text" id="question0-answer1" name="question0-answer1" />
  38.         <p><a href="javascript:" class="add-answer">Add Answer</a></p>
  39.     </div>
  40.    
  41.        
  42.     </div>
  43.    
  44.   </div>
  45.  
  46.   <input type="submit" value="go" name="submit">
  47.   </form>
  48.   <a href="#" onClick="addQuestion();" id="addQuestion" title="Add a question" class="link-sm" style="background-color:red;">Add Question</a>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement