Jump to content

Get Element Above Text


Recommended Posts

Hey all! I was wondering what the method would be to grab the element above an elements text would be or if its possible? <div id="content1" style="display:none;">Hello World</div><div id="clickme"></div> The reason I need to get the element above's text is I am already getting different information from "clickme"'s child elements... So in other words I can't nest "content1" inside of "clickme" so the whole next() and prev() methods won't work for my application. Thanks so much!

Link to post
Share on other sites

Not quite sure I understand your example, if the 'content1' element was nested inside, you would use children(), if you want to target all child elements, but exclude 'content1' you could use .not('#content1'), only with 'content1' above 'clickme' element would you use prev('#content1').

Link to post
Share on other sites

The reason I have it outside is because I'm using an api that needs the info thats nested and if I have extra info in there it messes it up. But I still need to add some custom info elsewhere on the page on a click function. Now I prefer to use jQuery. I believe I'm onto something with using .parent() some more tweaking and I think I'll have it. Will post my solution, also if someone else has any ideas let me know!

Link to post
Share on other sites

both elements shown are parents?, and since there can only be one element with this id ref, you usually just place code to select the above elements text AFTER or BEFORE, the child elements are processed similar to below var htmltext = $('div#content1').html();alert(htmltext)

Link to post
Share on other sites

Maybe not? I don't know. I just starting to get a grip on js and jquery $("div#clickme").click(function () {alert(document.getElementById("clickme").previousSibling);}); this alerts [object Text]... and $("div#clickme").click(function () {alert(document.this.previousSibling);}); alerts nothing.

Edited by chasethemetal
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...