Note that there are some explanatory texts on larger screens.

plurals
  1. POJquery - Get value from dynamically created inputs
    primarykey
    data
    text
    <p>I'm stuck in trying to create a "quiz-generator". </p> <p>I have a form were you can fill out answer alternatives for the quiz, but here you could also dynamically create new fields with new id's.</p> <p>So its creating new inputs with id txtAnswer1,txtAnswer2 etc.</p> <p>Here is the (not working) code to get the values:</p> <pre><code> $("#AddNewQuiz").click(function () { var NewQuizAlts = $("#txtAnswer").val(); var NewQuizQuestion = $("#NewQuizQuestion").val(); var init = { 'questions': [ { 'question': NewQuizQuestion, 'answersAlts': [NewQuizAlts,'Two','Three','Four'], 'correctAnswer': 1 } ] } </code></pre> <p>You see here it is only getting the static first value. But I would like to get all of the values created in a "loop" or something.</p> <p>Anyone has got any ideas?</p> <p>Thanks! </p> <hr> <p><strong>Thanks for the answers!</strong></p> <p>But I can't manage it to work!</p> <p>I've tried this:</p> <pre><code> $('input[id^="txtAnswer"]').each(function(input){ var value = $('input[id^="txtAnswer"]').val(); var id = $('input[id^="txtAnswer"]').attr('id'); alert('id: ' + id + ' value:' + value); }); </code></pre> <p>But is only giving me alerts (4 times) of the <strong>only the last</strong> created input. What can be wrong?</p> <p>Thing is, this is what I would like to achive:</p> <p>1.) Use the above code to get a list of <strong>all</strong> created inputs.</p> <p>2.) Put them in a comma seperated list with the values like this:</p> <pre><code> QuestionAlts = [''+Alt1+'',''+Alt2+'',''+Alt3+'']; </code></pre> <p>3.) And also have it generating more alternatives in the list depending on how many inputs that were created, so if I created 5 alternatives it will be:</p> <pre><code> QuestionAlts = [''+Alt1+'',''+Alt2+'',''+Alt3+'',''+Alt4+'',''+Alt5+'']; </code></pre> <p>I understand if it is a lot to ask for help with all this but a little bit is better than nothing :)</p> <p>Thanks!</p>
    singulars
    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.
    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