I have a div which contains a delete hyperlink, onlick of it, I want the text of the div tag. How is it possible in jquery?!
<div>sometext<a href="#">delete</a></div>
I want to get the text of div tag 'sometext' and if possible the id of that div too. Any ideas?!
Although adding in a span tag would probably more efficent
http://jsfiddle.net/tN8Mv/
jQuery skips over text nodes for all of its traversal methods ( except .contents() ). To access the previous sibling, then text in this case, regardless of nodeType, you can access the DOM node and then use the .previousSibling property. Then you can access the text of that element.
Access to the Id if the parent is then simple as well.
Sample fiddle page: http://jsfiddle.net/MarkSchultheiss/FuQ6g/
NOTE: one advantage of this method is if you have markup like:
the "stuff after" text is not selected.
The problem with doing:
is that it will get ALL the text within the div (sometext AND delete).
I'm assuming you only want the text in the div and not the link.
I've knocked up an example on jsFiddle:
http://jsfiddle.net/HK794/
Ideally you might want to wrap the text in a span, like:
Then your JavaScript would be:
EDIT
As @DarthJDG states if you don't want to change your markup, any these would also work:
jQueryElement.parent().text();
This sets content to the value of any direct children which are text.