Problem with execution:
$(this).parent().text();
is that it will get ALL the text in the div (sometext AND delete).
I assume that you only want text in a div, not a link.
I applied the jsFiddle example:
http://jsfiddle.net/HK794/
Ideally, you can wrap text in between, for example:
<div id="div1"><span>sometext</span><a href="#">delete</a></div>
Then your JavaScript will look like this:
$("a").click(function(e){ $div = $(this).parent("div"); id = $div.attr("id"); text = $div.find("span").text(); alert( text ); e.preventDefault(); });
EDIT
As pointed out by @DarthJDG, if you don't want to change your markup, any of these will also work:
text = $div.get(0).childNodes[0].nodeValue; //OR text = $div[0].childNodes[0].nodeValue; //OR text = $div.get(0).firstChild.nodeValue; //OR text = $div[0].firstChild.nodeValue; //OR //Gets the first text node $div.contents().filter(function() { return this.nodeType == Node.TEXT_NODE; }).text();
Tomgrohl
source share