Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how get all values of one class in jquery?

I am trying to get all values of a class input fields. but it is return only one fields value

Html code

<div id="mydiv">
<input class="seconds" type="text" value="40">
<input class="seconds" type="text" value="20">
<span class="countdown"></span>
</div>

Javascript code

<script src="{{ asset('js/vendor/jquery-3.1.0.min.js') }}"></script>
<script src="{{ asset('js/jquery.missofis-countdown.js') }}"></script>


<script>

var ek=$('.seconds').val();

console.log(ek);
$( '.countdown' ).countdown( {
  from: ek, // 3 minutes (3*60)
  to: 0, // stop at zero
  movingUnit: 1000, // 1000 for 1 second increment/decrements
  timerEnd: undefined,
  outputPattern: '$day Day $hour : $minute : $second',
  autostart: true,
});
</script>

I want to get values of both input fields having same class

like image 661
Bilal Arshad Avatar asked Sep 22 '19 14:09

Bilal Arshad


1 Answers

Use map() to create array of the values

var ek = $('.seconds').map((_,el) => el.value).get()

console.log(ek)
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="mydiv">
<input class="seconds" type="text" value="40">
<input class="seconds" type="text" value="20">
<span class="countdown"></span>
</div>
like image 106
charlietfl Avatar answered Nov 11 '22 05:11

charlietfl