JQuery - list of string

javascript, jquery

Solution

You are using equality comparion but you have to use wild card probably jquery attribute starts with `^` but the above statement will give value of first matched element. You can use each to iterate through all elements.

var s = $("[name^='CountAnswer']").val();

Iterating using each().

Live Demo

$("[name^='CountAnswer']").each(function(){
   alert($(this).val());
   //or
   alert(this.value);
});

Edit Based on OP comments. For getting the values of all matches.

Live Demo

strValues = $("[name^='CountAnswer']").map(function(){  
   return this.value;
}).get().join(',');

Problem

In JQuery, why do I get `information Undefined` with the following code? JS - right part is Undefined ``` var s = $("[name='CountAnswer']").val(); ``` HTML ``` <input style="width:150px" type="text" id="CountAnswer_1_" name="CountAnswer[1]"> <input style="width:150px" type="text" id="CountAnswer_2_" name="CountAnswer[2]"> <input style="width:150px" type="text" id="CountAnswer_3_" name="CountAnswer[3]"> ```

Original source