Jump to content

Get Element Above Text


chasethemetal

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 comment
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 comment
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 comment
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 comment
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.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...