glukkkk glukkkk - 6 months ago 25
jQuery Question

How to watch several tags using jQuery

Here is my HTML code:

<textarea id="uno-1" name="1"></textarea>
<textarea id="uno-2" name="2"></textarea>


And here is my JS code:

$("[id^=uno]").keyup(function() {
clearTimeout(typingTimer);
if ($("[id^=uno]").val) {
typingTimer = setTimeout(function() {
$.get("/someUrl", {
text: $("[id^=uno]").val(),
id: $("[id^=uno]").attr('name')
});
}, 1000);
}
});


The point is that text is saved automatically by sending it to GET Url right after typing it. However the problem is that it always catches only the first text area even if I type text in the second text area. I need to send GET attributes respectively. Is there a way to accomplish this?

Answer

because you are always referencing all the inputs and you are not using the current only.

$("[id^=uno]").keyup(function() {
    clearTimeout(typingTimer);
    var input = $(this);  //reference the current textarea
    if (input.val().length) {  //not sure what your check was, but it was strange
        typingTimer = setTimeout(function() {
            $.get("/someUrl", {
                text: input.val(),  //use the variable we set about
                id: input.attr('name')
            });
        }, 1000);
    }
});