Choosing the next space with className with jQuery selector

Fighting a jquery selector to select

<span class='textContent'> 

when the edit_click class is clicked. Any help would be greatly appreciated.

<span>
    <i class='fa fa-pencil edit_click'></i>
</span>
<span class='textContent'>Hello</span>

Javascript event handler

$('.edit_click').click(function (e) {
    $(this).next('span.textContent').attr('contenteditable', 'true');
    e.preventDefault();
});
+4
source share
2 answers

You need to use .parent()to get the parent’s link span, then .next()you can use on spanfor immediate targeting right after your sibling.

$(this).parent().next('span.textContent')
+6
source

closest() , span i. .next(), span.textContent , closest,

$('.edit_click').click(function (e) {
   $(this).closest('span').next(".textContent").attr('contenteditable', 'true');
});

e.preventDefault , i, .

+4

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


All Articles