javascript - How do I append text to many user inputs within the same html class? -


i have html form, has many inputs within same html class. when user presses submit button, need every input field submit whatever user wrote in them, , specific string. if user puts word "cat" in text box, form submit "catptq". have function sort of accomplishes this, instead of retaining user input, uses first instance of input in class. function have looks this.

$(function () {             $('#question_entry_submit_button').on('click', function () {                 var text = $('.ptq');                 text.val(text.val() + 'ptq');             });         }); 

this loop through html class of '.ptq' , append "ptq" end of user input, no matter user inputs second input box , onward, submits first user input + 'ptq' in place of actual user input + 'ptq'.

i think makes sense, can try , clarify if needed.

try use .val() 's callback function,

text.val(function(_,val){   return val + "ptq"; }); 

full code,

$(function() {    $('#question_entry_submit_button').on('click', function () {       var text = $('.ptq');       text.val(function(_,val){         return val + "ptq";       });    }); }); 

Comments

Popular posts from this blog

c# - Binding a comma separated list to a List<int> in asp.net web api -

Delphi 7 and decode UTF-8 base64 -

html - Is there any way to exclude a single element from the style? (Bootstrap) -