Jump to content

Adding or Subtracting elements based off user selection


Xtremer360

Recommended Posts

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');    
        }
    });

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.