Note that there are some explanatory texts on larger screens.

plurals
  1. POJquery append - appending double?
    primarykey
    data
    text
    <p>Why is this code appending the object twice for each element?</p> <pre><code>$('input[data-type="answer"]').each(function (index, input) {     var name = $(input).attr('name');     var value = $(input).val();     $("#QuizQuestion").append('&lt;div id="QuizQuestionHeadline"&gt;&lt;/div&gt;&lt;div class="control-group"&gt;&lt;label class="radio"&gt;&lt;input type="radio" class="input-fluid" value="' + value + '" /&gt;' + value + '&lt;/label&gt;&lt;/div&gt;'); }); </code></pre> <p>Thanks!</p> <hr> <p>Realized I published wrong code, here it is:</p> <p>Jquery to create the answers:</p> <pre><code>var newAnswerAlt = "appendAnswerAltRadio" + i++; $("&lt;div&gt;&lt;div class='controls'&gt;&lt;input type='text' class='answerAltRadiobtn' data-type='answer' class='input-fluid' name='txtAnswer"+ n++ +"'&gt; &lt;label class='checkbox'&gt;&lt;input type='checkbox' id='QuizCheckbox"+ c++ +"'&gt; Correct Answer&lt;/label&gt;&lt;/div&gt;&lt;/div&gt;").attr({id: newAnswerAlt, class: ''}).prependTo(".NewQuizAnswersGroup").hide().fadeIn('slow'); }); </code></pre> <p>Jquery to get the attributes from the created answers and append them to another element:</p> <pre><code> $('input[data-type="answer"]').each(function(index, input){ var name = $(input).attr('name'); var value = $(input).val(); var question = "txtQuestion1"; $("#QuizQuestion").append('&lt;div id="QuizQuestionHeadline"&gt;&lt;/div&gt;&lt;div class="control-group"&gt;&lt;label class="radio"&gt;&lt;input type="radio" class="input-fluid" name="' + question + '" value="'+value+'" /&gt;'+value+'&lt;/label&gt;&lt;/div&gt;'); }); var NewQuizQuestion = $("#NewQuizQuestion").val(); $("#QuizQuestionHeadline").append("&lt;legend&gt;"+NewQuizQuestion+"&lt;/legend&gt;"); $('input[data-type="UserDetails"]').each(function(index, input){ var name = $(input).attr('name'); var label = $("label[for='"+name+"']").text() var value = $(input).is(":checked"); if($(input).is(":checked")){ $("#formAlts").append('&lt;div id="formHeader"&gt;&lt;/div&gt;&lt;div class="control-group"&gt;&lt;label&gt;'+label+'&lt;/label&gt;&lt;div class="controls"&gt;&lt;input type="text" class="input-fluid" name="' + name + '" /&gt;&lt;/div&gt;&lt;/div&gt;'); } }); var formHeader = "Fill out form"; $("#formHeader").append("&lt;legend&gt;"+formHeader+"&lt;/legend&gt;"); var terms = "Accept Terms &amp; Conditions"; $("#terms").append("&lt;div class='control-group'&gt;&lt;label class='checkbox'&gt;&lt;input type='checkbox' name='txtTerms'&gt;"+terms+"&lt;/label&gt;&lt;/div&gt;"); </code></pre> <p>The second call for "'input[data-type="UserDetails"]'" works fine but not for "'input[data-type="answer"]'".</p> <p>Can't really understand it!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload