Leo Delkin
Leo Delkin

Reputation: 73

Use next and prev() in jquery

<label for="a1">First:</label>
<input id="a1" class="first" type="text" name="a1" value="123">
<label for="b1">Second:</label>
<input id="b1" class="second" type="text" name="b1"> <br />
<label for="a2">First:</label>
<input id="a2" class="first" type="text" name="a2"value="3424">
<label for="b2">Second:</label>
<input id="b2" class="second" type="text" name="b2"> <br />
<label for="a3">First:</label>
<input id="a3" class="first" type="text" name="a3"value="1235">
<label for="b3">Second:</label>
<input id="b3" class="second" type="text" name="b3"> <br />

$('.second').click(function(){
   var get_first = // ???
   $(this).val(get_first);
})

i would like for example - if i click on input b3 then in this value now is value from a3. how can i make it with next and prev in one class?

http://jsfiddle.net/Bgkrz/

Upvotes: 0

Views: 127

Answers (3)

Ranganadh Paramkusam
Ranganadh Paramkusam

Reputation: 4368

Check this Fiddle

Use jQuery.prevAll() It'll return the array in reverse order, you can find the element at zero index here.

Upvotes: 0

David Thomas
David Thomas

Reputation: 253486

I'm not sure I can think of a reason for doing this, but assuming I understand your requirements correctly, then this should work (currently untested):

$('input:text').focus(
    function(){
        var that = $(this);
        that.val(that.prevAll('input:text:first').val());
    });

JS Fiddle demo.

References:

Upvotes: 2

Ravi Gadag
Ravi Gadag

Reputation: 15881

you can try like this JsFiddleDemo

$('.second').click(function(){
   var get_first = $(this).prev().prev().val();
   $(this).val(get_first);
})

Upvotes: 0

Related Questions