0

I have a parent div named "master_ts1891_tab_2901" with many child divs under it. I need a function that would return the number of times the string "No Records Found" occurs in the entire parent div.

This string may be under any child div. Note that the string "No Records Found" is a simple text.

Since the entire code is huge, I have addded the sample markup.

Here a code sample:

<div id="master_DefaultContent_rts_ts1891_tab_2901">
    <div id="master_DefaultContent_rts_ts1891_s5928" class="section ViewEditSection  hidden" layoutid="5928">
        <div id="master_DefaultContent_rts_ts1891_s5928_up">
        ...
        ...many more divs and tables
        ...
            <td colspan="8" style="text-align:left;"><div>No Records Found</div></td>

        ...many more divs and tables
        ...
            <td colspan="8" style="text-align:left;"><div>No Records Found</div></td>
    <div>

jquery or javascript both will do.

Please help me out.

0

5 Answers 5

5

Use below code using jQuery/Javascript.

DEMO

$(document).ready(function(){
  var childText = $('#master_DefaultContent_rts_ts1891_tab_2901').text();
  var count = (childText.match(/No Records Found/g) || []).length;
  alert(count);
});
1
  • Use JavaScript var childText = document.getElementById("master_DefaultContent_rts_ts1891_tab_2901").innerText: Jun 2, 2015 at 13:22
2

Or you could use the jQuery "Contains" filter:

jQuery(":contains('No Records Found')")
1
  • 1
    I think this will not return the number of times the string is repeated inside a div
    – Khalid
    Jun 2, 2015 at 12:57
1

This code may help you, I didn't test but if it doesn't work it can give you an idea :

var count = $("#master_DefaultContent_rts_ts1891_tab_2901").text().split('No Records Found').length - 1
1

I would use the contains jQuery filter as Pensan mentioned above - this seems like the simplest way. The following would give you the number that you're after:

$('#parentDiv').find(":contains('No Records Found')").length

Or if you prefer

$("#parentDiv div:contains('No Records Found')").length
0

Try this..

alert($("#master_DefaultContent_rts_ts1891_tab_2901 div:contains('No Records Found')").length);
<div id="master_DefaultContent_rts_ts1891_tab_2901">
	<div>No Records Found</div>
	<div>No Records Found</div>
</div>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.