Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selecting next Span with className with Jquery selector

Tags:

jquery

Struggling with Jquery selector to select

<span class='textContent'> 

when 'edit_click' class is clicked. Any help would be highly 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();
});
like image 476
InTheWorldOfCodingApplications Avatar asked Dec 23 '22 21:12

InTheWorldOfCodingApplications


2 Answers

You need to use .parent() to get the reference of parent span, then .next() can be used on span to target immediately following sibling.

$(this).parent().next('span.textContent')
like image 154
Satpal Avatar answered Feb 22 '23 23:02

Satpal


Try to use the closest() function at this context to select the parent span element of i. After that use .next() since span.textContent is a sibling to the element which was previously selected with closest,

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

And using e.preventDefault is pointless here as you bound event over an i tag, since it doesn't have any built in functionality with it.

like image 25
Rajaprabhu Aravindasamy Avatar answered Feb 23 '23 01:02

Rajaprabhu Aravindasamy