0

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.

2
  • 1
    It should be $(current).find('.fieldA').val($(previous).find('.fieldA').val()); Commented Mar 30, 2012 at 21:47
  • Maybe run a debug on the code and add a watch on the value of rowId and current. See if the value of rowId is being set to a value that is beyond the number of elements being returned by the expression. And guy above is correct, because you can't assign val() with = you need to pass it value to be assigned. Commented Mar 30, 2012 at 21:50

2 Answers 2

3

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());
Sign up to request clarification or add additional context in comments.

5 Comments

That's good to know but I'm still getting $current is not defined. I think line 2 is failing.
As you can see, there is no $current in your code. So the error is not caused by the code you posted.
I know that's part of my confusion. What's up there is my code just with different class names. I don't know if FireFox's error console is being weird or what.
I'd suggest you to copy the actual error instead of just typing/copying the error message... You might also want to use the "break on error" feature of Firebug.
Ok nevermind. It was a PEBKAC issue. Your fix to my original code solved the problem. Thank you.
-1

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());
    });
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.