'Get text between two elements JQUERY
I have the following code in my application.
<div title="Please Click Here to Expand" class="technology3 closedlanguage" headerindex="0h">
<span class="accordprefix">
<img src="http:www.test.com/expanded.gif" style="width:13px; height:13px; margin-Right:20px">
</span>
Need this text<span class="accordsuffix"></span></div>
I need to get text between "accordprefix" and "accordsuffix" classes which is "Need this text"
for this i did following code.
$(window).load(function(){
$(".accordprefix").each(function(){
alert($(this).next().html());
});
});
but its not working.Please give any suggestions.
Solution 1:[1]
Solution 2:[2]
how about:
console.log( $('.technology3').text() );
since it's the only text content in the surrounding div, that would work.
Solution 3:[3]
$('.technology3).text().trim();
You want to call trim() on it to clear out the garbage white space that surrounds it.
Solution 4:[4]
Try this if div.technology3 are multiple,
$(".accordprefix").each(function(){
alert($(this).closest('.technology3').text());// get the closest div.technology3 text
});
if div.technology3 are single then try this,
alert($('.technology3').text());
Solution 5:[5]
Try this:
$('.technology3').text();
Solution 6:[6]
I would add 2 markers (just a small string like '~~~~'). First AFTER span.accordprefix, second BEFORE span.accordsuffix. Then we need to get text content of div.technology3, find text between our markers and store it. Then we need to remove markers.
Check .after() and .before()
Solution 7:[7]
You can achieve this using jQuery .nextUntil()
$("#id_of_first_ele").nextUntil("#id_of_second_ele").text().trim()
So in your case it will be
$(".accordprefix").nextUntil(".accordsuffix").text().trim()
This will work jQuery 1.4 onwards.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Arun P Johny |
| Solution 2 | mgherkins |
| Solution 3 | Mike Thomsen |
| Solution 4 | Rohan Kumar |
| Solution 5 | isherwood |
| Solution 6 | piyushj |
| Solution 7 | Hari Das |
