JQuery - search for the previous selector relative to the current

I have a form with three sections. Each section has a dedicated div for fairly long contextual help messages regarding input that has focus.

My question is, if I give each of these prompts divs a form-tip class and every input that has a has-tip prompt, how can I get a prompt to always appear in the previous “form-tip” "div?

Sort of:

$('.has-tip').each(focus(function() {
    var tip = $(this).attr('title');
    // Find most recent $('.form-tip') in the DOM and populate it with tip
}));

Hope this makes sense ... Thanks for any help.

+3
source share
2 answers

.has-tip .form-tip, .closest() :

$('.has-tip').each(focus(function() {
    var tip = $(this).attr('title');
    var ft = $(this).closest('.form-tip');
    // Find most recent $('.form-tip') in the DOM and populate it with tip
}));

, .prev('.form-tip'), , .prevAll('.form-tip: first').

+2
var container = $(this).closest('.form-tip')

.each, .

+1

Source: https://habr.com/ru/post/1749862/


All Articles