I am trying to figure out how to use jquery to reference top level object in html (body) from any child element inside html.
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
I’m not clear why $(“body”) won’t work.
see http://api.jquery.com/element-selector/
This can be used from anywhere and what child you are “in” does not matter.
Additional notes based on comments:
It really depends on each case. If the need is relative (there is more than one table in the page) then using
parent()andclosest()will often be the best way. In simpler pages, you can search for elements directly by ID.Two examples:
A page with two lists where you want to move elements between one list and the other. Since this functionality is defined as a page having just two lists then it makes sense to use the direct method.
A page with a dynamic list of users and dynamic addition of elements in a user’s content (eg dynamic chat). In this case you want to use relative since each user’s area will have the same elements and you want to modify that area relative to the user area.