Xtremer360 Posted May 4, 2011 Share Posted May 4, 2011 What I'm wanting to do is have it get the current numAnswers and if the user decides to change the number of numAnswers then either add the difference or subtract the difference. And I"m not sure what I need to do. $('#numAnswers').change(function() { var numAnswers = $("select#numAnswers").val(); var i = 0 var answerNum = i + 1; for (i = 0; i < numAnswers; i++) { $('<div class="field required answers"><label for="answer'+ answerNum +'">Answer #'+ answerNum +'</label><input type="text" class="text" name="answer'+ answerNum +'" id="answer'+ answerNum +'" title="Answer '+ answerNum +'"/><span class="required-icon tooltip" title="Required field - This field is required, it cannot be blank, and must contain something that is different from emptyness in order to be filled in. ">Required</span>').appendTo('.answerLeg'); } }); Quote Link to comment https://forums.phpfreaks.com/topic/235523-adding-or-subtracting-elements-based-off-user-selection/ Share on other sites More sharing options...
Xtremer360 Posted May 4, 2011 Author Share Posted May 4, 2011 To update: What I'm attempting to do is after the user makes a selection from the dropdown it adds some elements to the fieldset which it does and keeps a count of how many there are. It does that much correctly but the additional step I want to take is if the user decides to change that selection in the dropdown it'll only add or subtract to have that many new elements in the fieldset. So for example if the user originally selects 2 from the dropdown then it adds to divs to the fieldset but then if the user changes the dropdown to 4 then it will add ONLY two more divs to make that total amount of divs be 4 and vice versa if the user selects 1 then it will take away 1 to have 1 be its count for how many divs it should have. $('#numAnswers').change(function() { var numAnswers = $("select#numAnswers").val(); var i = 0 for (i = 0; i < numAnswers; i++) { var answerNum = i + 1; $('<div class="field required answers"><label for="answer'+ answerNum +'">Answer #'+ answerNum +'</label><input type="text" class="text" name="answer'+ answerNum +'" id="answer'+ answerNum +'" title="Answer '+ answerNum +'"/><span class="required-icon tooltip" title="Required field - This field is required, it cannot be blank, and must contain something that is different from emptyness in order to be filled in. ">Required</span>').appendTo('.answerLeg'); } var answerCount = $("fieldset.answerLeg div").size(); if( answerCount < answerCount ) { $("div#answers").remove(); } }); <div class="field required answers"> <label for="numAnswers">Number of Answers</label> <select class="dropdown" name="numAnswers" id="numAnswers" title="Number of Answers"> <option value="">- Select -</option> <?php $numAnswers = array("1","2","3","4","5"); foreach($numAnswers as $nA): ?> <option value="<?php echo $nA; ?>"><?php echo $nA; ?></option> <?php endforeach; ?> </select> <span class="required-icon tooltip" title="Required field - This field is required, it cannot be blank, and must contain something that is different from emptyness in order to be filled in. ">Required</span> </div> <fieldset class="answerLeg"> <legend>Add Poll Answers</legend> </fieldset> Quote Link to comment https://forums.phpfreaks.com/topic/235523-adding-or-subtracting-elements-based-off-user-selection/#findComment-1210472 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.