Ok, so after some rework based on your comments, I think I put together what you need: Fiddle
The $(document).on(...) line is meant to handle dynamically added elements
$(document).ready(function(){
$(document).on('click','.answers p',function()
{
var $parent = $(this).parent();
var response = $parent.data("specific");
$(this).append(response);
$parent.siblings('.answers').remove();
I've kept your code the same up to this point. I first replace the .answers class with a different class so that the user cannot click on the elements again that they have already made a selection for.
$('.answers').addClass('selectedAnswer').removeClass('answers');
Next, I put in some pseudo-logic to create your next set of questions, at the 'newAnswers' variable. You can replace this with a function, or some ajax call, whatever you need.
//do work to get next questions
var newAnswers = '<div class="question-text"> <p>Next question?</p> </div><div class="answers" data-specific="Next First Response"> <p>Next First answer</p> </div>';
I then append the new set of questions to the question class element
$('.question').append(newAnswers);
});
});
The recursion works due to using the $(document).on logic, in addition to swapping out the selected answer class and append the new answers. What happens is this:
- Page displays initial question set
- Document is listening for any click events on an .answer p selector
- User selects some answer
- Remaining answers disappear, new question and answers are added
- Process repeats to step 3
$(document).ready(function(){
$(document).on('click','.answers p',function()
{
var $parent = $(this).parent();
var response = $parent.data("specific");
$(this).append(response);
$parent.siblings('.answers').remove();
$('.answers').addClass('selectedAnswer').removeClass('answers');
//do work to get next questions
var newAnswers = '<div class="question-text"> <p>Next question?</p> </div><div class="answers" data-specific="Next First Response"> <p>Next First answer</p> </div>';
$('.question').append(newAnswers);
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="question">
<div class="question-text">
<p>First question?</p>
</div>
<div class="answers" data-specific=" First Response">
<p>First answer</p>
</div>
<div class="answers" data-specific=" Second Response">
<p>Second answer</p>
</div>
<div class="answers" data-specific=" Third Response">
<p>Third answer</p>
</div>
</div>