2

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.

user2952488
  • 21
  • 1
  • 2
  • Because it is not wrapped in a specific object it is within parent element of the `.accordprefix` span. You could do `$(".accordprefix").each(function(){$(this).parent().html();});` but then you would need to filter out all the other HTML you do not need. – Nunners Nov 04 '13 at 12:25
  • You have more than one accordprefix and accordsuffix elements ? – Joao Paulo Nov 04 '13 at 12:31
  • See https://stackoverflow.com/questions/25873650/jquery-nextuntil-include-text-nodes – Jake Dec 20 '22 at 00:46

7 Answers7

6

You can also look at

$.trim($($('.accordprefix')[0].nextSibling).text())

Demo: Fiddle

Arun P Johny
  • 384,651
  • 66
  • 527
  • 531
5

how about:

console.log( $('.technology3').text() );

since it's the only text content in the surrounding div, that would work.

gherkins
  • 14,603
  • 6
  • 44
  • 70
2
$('.technology3).text().trim();

You want to call trim() on it to clear out the garbage white space that surrounds it.

Mike Thomsen
  • 36,828
  • 10
  • 60
  • 83
1

Try this if div.technology3 are multiple,

$(".accordprefix").each(function(){
    alert($(this).closest('.technology3').text());// get the closest div.technology3 text
});

Read closest() and text()

if div.technology3 are single then try this,

alert($('.technology3').text());

Demo

Rohan Kumar
  • 40,431
  • 11
  • 76
  • 106
1

Try this:

$('.technology3').text();
isherwood
  • 58,414
  • 16
  • 114
  • 157
Md Nasir Uddin
  • 2,130
  • 8
  • 38
  • 59
0

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()

piyushj
  • 1,546
  • 5
  • 21
  • 29
Michael S
  • 7
  • 2
0

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.

Hari Das
  • 10,145
  • 7
  • 62
  • 59