Taylor Gomez
Taylor Gomez

Reputation: 330

Get input value except the last by jquery

I want getting all value in inputs except the last input in last class .tr by jquery, but it don't work for me, How can fix it?

I try as:

DEMO: http://jsfiddle.net/d4xZK/

HTML:

<div class="tr">
    <input type="text" value="111">
</div>
<div class="tr">
    <input type="text" value="222">
</div>
<div class="tr">
    <input type="text" value="333">
</div>
<div class="tr">
    <input type="text" value="444">
</div>

jQuery:

$('.tr').each(function(){
    var mpiVal = $('.tr input').not(':last').val();
    alert(mpiVal)
)}

Upvotes: 2

Views: 724

Answers (5)

Ja͢ck
Ja͢ck

Reputation: 173562

You can slice() off the last one:

$('.tr').slice(0, -1).each(function() {
    var mpiVal = $('input', this).val();
    console.log(mpiVal);
});

Demo

Upvotes: 3

Byscripts
Byscripts

Reputation: 2588

$(".tr:not(:last) input").map(function(){ return $(this).val() }).get()

Returns ["111", "222", "333"]

Upvotes: 0

Exception
Exception

Reputation: 8379

There is a syntax mistake in last line

  var arr = []
  $('.tr:not(:last)').each(function(){
     arr.push.call(arr,$(this).find('input').first().val());         
  });
  alert(arr);

Upvotes: 0

Diabolic
Diabolic

Reputation: 458

Or you can simply use the right selectors for that;

$('.tr:not(:last) input').each(function(){
    var mpiVal = $(this).val();
    alert(mpiVal);
});

Upvotes: 1

Anton
Anton

Reputation: 32581

$('.tr').not(':last').each(function(){
    var mpiVal = $(this).children('input').val();
    alert(mpiVal)
});

Demo here http://jsfiddle.net/d4xZK/4/

Upvotes: 2

Related Questions