WORKING OF JQUERY PREV() FUNCTION

The .closest và .find selectors are complements of each other & used together are the best way to lớn get to the corresponding element of where the click (or any event) occurred.

Bạn đang xem: Working of jquery prev() function

From the documentation (paraphrased):

The .closest selector traverses up the DOM to lớn find the parent that matches the conditions. the .find selector traverses down the DOM where the event occurred, that matches the conditions.

Now, given these and since the sự kiện is always passed as the attribute lớn the bound function, following is the simplest, best method khổng lồ get lớn the corresponding element somewhere:

data = $(event.target).closest("https://obatambeienwasirherbal.com/find-previous-div-with-class-jquery/imager_1_22385_700.jpgelement-row"https://obatambeienwasirherbal.com/find-previous-div-with-class-jquery/imager_1_22385_700.jpg).find("https://obatambeienwasirherbal.com/find-previous-div-with-class-jquery/imager_1_22385_700.jpg.title"https://obatambeienwasirherbal.com/find-previous-div-with-class-jquery/imager_1_22385_700.jpg); If you got what I am saying, that is all. But I will try to explain it with an example for those it is not already clear.

Let"https://obatambeienwasirherbal.com/find-previous-div-with-class-jquery/imager_1_22385_700.jpgs consider the following HTML; & let"https://obatambeienwasirherbal.com/find-previous-div-with-class-jquery/imager_1_22385_700.jpgs say you want the title nội dung on clicking Send email.

Xem thêm: Bí Quyết Đọc Sách Tiếng Anh Hiệu Quả Nhờ Đọc Sách, Kinh Nghiệm Không Bỏ Cuộc Khi Đọc Sách Tiếng Anh



*
Title Goes here

description goes here

Get title

The simplest way to lớn get the title on clicking the button, would be the following:

title = $(event.target).closest("https://obatambeienwasirherbal.com/find-previous-div-with-class-jquery/imager_1_22385_700.jpgelement-row"https://obatambeienwasirherbal.com/find-previous-div-with-class-jquery/imager_1_22385_700.jpg).find("https://obatambeienwasirherbal.com/find-previous-div-with-class-jquery/imager_1_22385_700.jpg.title"https://obatambeienwasirherbal.com/find-previous-div-with-class-jquery/imager_1_22385_700.jpg); of course, where the "https://obatambeienwasirherbal.com/find-previous-div-with-class-jquery/imager_1_22385_700.jpg.descr-button"https://obatambeienwasirherbal.com/find-previous-div-with-class-jquery/imager_1_22385_700.jpg is bound to the function in the $(document.ready)

$("https://obatambeienwasirherbal.com/find-previous-div-with-class-jquery/imager_1_22385_700.jpgbody"https://obatambeienwasirherbal.com/find-previous-div-with-class-jquery/imager_1_22385_700.jpg).on("https://obatambeienwasirherbal.com/find-previous-div-with-class-jquery/imager_1_22385_700.jpgclick"https://obatambeienwasirherbal.com/find-previous-div-with-class-jquery/imager_1_22385_700.jpg, "https://obatambeienwasirherbal.com/find-previous-div-with-class-jquery/imager_1_22385_700.jpg.descr-button"https://obatambeienwasirherbal.com/find-previous-div-with-class-jquery/imager_1_22385_700.jpg, do_something); Related protips: fatal: refusing lớn merge unrelated histories


#jquery
#events
#simple
#javascript
Written byLakshman
Say Thanks
Respond
Related protips

JavaScript iterate through object keys và values


573.8K
7

jQuery: When to lớn use $(document).ready() & when $(window).load()


188.4K
3

creating DOM elements with jQuery in a more elegant way


140.7K
13
2 Responses
Add your response
*

smith2martin

Typo. "https://obatambeienwasirherbal.com/find-previous-div-with-class-jquery/imager_1_22385_700.jpg.closest("https://obatambeienwasirherbal.com/find-previous-div-with-class-jquery/imager_1_22385_700.jpgelement-row"https://obatambeienwasirherbal.com/find-previous-div-with-class-jquery/imager_1_22385_700.jpg)"https://obatambeienwasirherbal.com/find-previous-div-with-class-jquery/imager_1_22385_700.jpg should be "https://obatambeienwasirherbal.com/find-previous-div-with-class-jquery/imager_1_22385_700.jpg.closest("https://obatambeienwasirherbal.com/find-previous-div-with-class-jquery/imager_1_22385_700.jpg.element-row"https://obatambeienwasirherbal.com/find-previous-div-with-class-jquery/imager_1_22385_700.jpg)"https://obatambeienwasirherbal.com/find-previous-div-with-class-jquery/imager_1_22385_700.jpg.


over 1 year ago·
dominicrose

closest actually exists in native javascript now, and querySelectorAll behaves like find. If you"https://obatambeienwasirherbal.com/find-previous-div-with-class-jquery/imager_1_22385_700.jpgre using jQuery youre probably interested in.siblings( )` which is kind of between closest and find.


over 1 year ago·
Have a fresh tip? share with obatambeienwasirherbal.com community!
Post
Post a tip
Best #Jquery Authors
*

steveniseki
578.6K
#jquery
#javascript
#css
*

becomingGuru
454.4K
#jquery
#Python
#JavaScript
*

dizpers
195.3K
#jquery
#requireJS
#css3
*

dperrymorrow
148.6K
#jquery
#Javascript
#PHP
prudnikov
130.3K
#jquery
#Python
#Django
Related Tags
#jquery
#events
#simple
#javascript
Sponsored by
#native_company#
#native_desc#
#native_cta#
Filed Under
Javascript Tips lớn Beat the DOM Into Submission
Awesome Job
See All Jobs
Full Stack Developer
CELEBREAK S.L
·
Spain
·
Full Time
Post a job for only$299
Sponsored by #native_company# — Learn More
#native_title##native_desc#
#native_cta#
obatambeienwasirherbal.com
APM product comparisonsContactPrivacyTerms
This site is protected by reCAPTCHA & the GooglePrivacy PolicyandTerms of Serviceapply.