chasethemetal Posted December 13, 2011 Share Posted December 13, 2011 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 More sharing options...
justsomeguy Posted December 13, 2011 Share Posted December 13, 2011 You can use the nextSibling and previousSibling properties to access adjacent nodes. https://developer.mozilla.org/En/DOM/Node.nextSiblinghttps://developer.mozilla.org/en/DOM/element.previousSibling Link to comment Share on other sites More sharing options...
dsonesuk Posted December 13, 2011 Share Posted December 13, 2011 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 More sharing options...
chasethemetal Posted December 13, 2011 Author Share Posted December 13, 2011 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 More sharing options...
dsonesuk Posted December 13, 2011 Share Posted December 13, 2011 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 More sharing options...
chasethemetal Posted December 13, 2011 Author Share Posted December 13, 2011 Sorry I needed to explain more content1 and clickme are dynamic meaning, there are lot's so I need to grab it in a (this) manner. Thanks for that though! Link to comment Share on other sites More sharing options...
justsomeguy Posted December 13, 2011 Share Posted December 13, 2011 Is there a problem using this.previousSibling? Link to comment Share on other sites More sharing options...
chasethemetal Posted December 13, 2011 Author Share Posted December 13, 2011 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 More sharing options...
chasethemetal Posted December 13, 2011 Author Share Posted December 13, 2011 Annddd $("div#clickme").click(function () {alert(this.previousSibling);}); alerts [object Text]; Link to comment Share on other sites More sharing options...
chasethemetal Posted December 13, 2011 Author Share Posted December 13, 2011 got it!!! $("clickme").click(function () {var info = $(this).prev().text();alert(info);}); Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.