0

I am new to jQuery and hope someone here can help me with this:

Basically what I am trying to do is the following:

  1. Check for all elements with the class "limit" and get their maxlength.
  2. Find an element with the class "count" in the same row.
  3. Add the found maxlength to the count element's text.

I am using the following but this displays the last maxlength for all the elements in question. My guess is that my last element is perhaps overwriting the others. Can someone tell me what I have to change here ?

var limit = '';
$('.limit').each(function() {
    limit = $(this).attr('maxlength');
    $(this).closest('tr').find($('.count').text(limit));
});

Many thanks in advance, Mike.

2
  • 1
    $(this).closest('tr').find('.count').text(limit); Commented May 8, 2014 at 15:42
  • 1
    $('tr .count').text(function() { return $(this).closest('tr').find('.limit').attr('maxlength'); }); Commented May 8, 2014 at 15:47

1 Answer 1

1

Try to use:

$(this).closest('tr').find('.count').text(limit);

You don't need to convert .count to jQuery object inside .find() method. You also need to put .text() outside of .find()

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot, that makes sense ! Will accept as soon as I can.

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.