William
William

Reputation: 1467

Using Jquery to copy the values of multiple fields

I'm trying to write a jquery funciton that will copy specified fields from the element above the current one into matching fields of the current element. Specifically I have a asp.net repeater that is spitting out tables with a few fields in it and I want a "Same as above" function. I'm still new to jquery though and I'm having some trouble with it. Here is some psudo code for what I'm currently trying to do feel free to propose a better method if you know it or simply fix this to work.

function CopyPrevious(sender, rowId) {
    var current = $(sender).closest('#wrappingDiv').find('.containingTable').eq(rowId);
    var previous = $(sender).closest('#wrappingDiv').find('.containingTable').eq(rowId - 1);
    $(current).find('.fieldA').val($(previous).find('.fieldA').val());
}

wrappingDiv is just a div I put around the table so I could find it with "closest" and each table has the class "containingTable". I put "fieldA", "fieldB", etc as class names on the fields so I could find them to get the values.

The issue I'm having is a javascript error on row 4 of the above: $current is not defined

EDIT: Updated the line 4 per the comments. It works now. Thank you.

Upvotes: 0

Views: 178

Answers (2)

devstruck
devstruck

Reputation: 1507

Not precisely sure I follow, but would this work for you?

function CopyPrevious(sender, rowId) {
    var current = $(sender).closest('.wrappingDiv').find('.containingTable input');  
    var previous = $(sender).closest('.wrappingDiv').prev().find('.containingTable input');
    current.each(function(){
        ($this).val(previous.is('.' + $(this).attr('class')).val());
    });
}

Upvotes: -1

ThiefMaster
ThiefMaster

Reputation: 318488

You cannot use .val() as an lvalue. To set a new value pass it as an argument instead:

$(current).find('.fieldA').val($(previous).find('.fieldA').val());

Upvotes: 3

Related Questions