How to get all value from input

I have the following sample HTML:

<table id="modem_list">
   <tr class="input_1" name="modem">
      <td>iPad 1<input type="hidden" value="1" class="output_1"></td>
   </tr>
   <tr class="input_2" name="modem">
      <td>iPad 2<input type="hidden" value="2" class="output_2"></td>
   </tr>
   <tr class="input_3" name="modem">
      <td>iPad 3<input type="hidden" value="3" class="output_3"></td>
   </tr>
</table>

And jQuery script:

val = [];
jQuery('input[class^=ouput_]').each(function(i){
   val[i] = jQuery(this).val();
});

I am getting an error when using this script, how to fix it?

Answers


try like so

var val = [];
$('input[class^="output_"]').each(function(){
   val.push($(this).val());
});
  1. you wrote ouput instead of output
  2. wrap the attribute value in quotes ""
  3. (and using push there's no need to use an explicit index for the array)

While the other answers are perfectly valid, here's a slightly shorter alternative using map, with get to return an array:

var val = jQuery('input[class^=output_]').map(function() {
    return this.value;
}).get();

Notice that in your code you have written 'ouput' instead of 'output'.


you could have used the same class, you are using wrong class name try:

var valArr = [];
jQuery("input[class^='output_']").each(function(){
   valArr.push(jQuery(this).val());
});

You can use:

var val= $("#modem_list :input").serializeArray();

Look, you have

...class^=ouput_ 

instead of

...class^=output_

you can also write this

val = [];
jQuery('input[class^=output_]').each(function(i){
   val.push(jQuery(this).val());
});

Need Your Help

Pass custom class data between view controllers in Swift

ios uitableview class swift viewcontroller

I have two view controllers one that contains a tableview and another that contains the page to show following selection of a particular row.

What should happen when a reference is deleted?

.net vb.net dll reference

I have a vb.net 3.5 application which references a dll (abc.dll, also in .net 3.5)