Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am using the following formular:

ROW1: <input class="search" name="pos[]" value=""><input name="str[]" value="">
ROW2: <input class="search" name="pos[]" value=""><input name="str[]" value="">
ROW3: <input class="search" name="pos[]" value=""><input name="str[]" value="">

For the first input of each row I activated jquery autocomplete:

$(".search").autocomplete(ac_new_invoice);
var ac_new_invoice = {
    source: "productsearch.php",
    minLength:1,
    select: function(data){
        var str = document.forms[0].elements["str[]"];
        str[1].value = '1.000,00';
    }
}

So it works fine when I type

 str[1].value = '1.000,00';

But what i want is that if I type in row 1 the input str in row 1 will be changed. But how do I get the row in which the autocomplete is performed from?

Thanks in advance!

share|improve this question
    
Have you tried the currently active element? $( document.activeElement ) –  dc5 Jun 8 '13 at 14:09

1 Answer 1

up vote 0 down vote accepted

Rather than apply autocomplete to all the elements at once, you can call it on each element.

$('.search').each(function (i) {
    var ac_new_invoice = (function (i) {
        return {
            source: "productsearch.php",
            minLength: 1,
            select: function (data) {
                var str = document.forms[0].elements["str[]"];
                str[i].value = '1.000,00';
            }
        };
    })(i);
    $(this).autocomplete(ac_new_invoice);
});

Note that you'll need (function (i){ blabla })(i) to make a immediate copy of the variable i.

share|improve this answer
    
Just changed this line: str[i+1].value = '1.000,00'; Thank you very much!!! –  user1137370 Jun 8 '13 at 14:24

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.