1

I have so many DIV which have <p> and I want to set .each() for particular DIV's <p> for that I have done below code.

var Selected_type = 'business'; //which comes dynamically
var para_id = Selected_type + '_ltr';

var text = "";
$(para_id + "p").each(function(index) {
  if (index < count) {
    text += '<p>' + ($(this).text()).trim() + "</p><br>";
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<div class="col-xs-12" id="business_ltr" style="max-height: none; display: none;">
  <p>Learn how to transform your life for the better with 16 of the Web’s favorite entrepreneurship and leadership articles from this year.</p>

  <p>Mental fortitude is essential for entrepreneurship, and in this wildly popular Forbes article, </p>
</div>

<div class="col-xs-12" id="study_ltr" style="max-height: none; display: none;">
  <p>With all the emphasis placed on education in our society it is remarkable to realize that there has never been an actual technology of study or a technology of education. </p>

  <p>The first little gate that has to be opened to embark upon study is the willingness to know. .</p>
</div>

but this shows me undefined and doesn't show me any result.

but if I set $("p" ).each() it is working but it is get all <p> but I want particular DIV's <p>

can anybody help me with this

Abhishek Pandey
  • 12,147
  • 8
  • 31
  • 60
MNJ
  • 599
  • 1
  • 10
  • $("div p" ).each() ? – MadaManu Jan 08 '20 at 05:24
  • 1
    You're trying to select an ID, so you need to put `#` in front of the selector string. The IDs also don't end with `p`, so remove the `+"p"` part. But IDs should be *unique* in a document, so `.each` shouldn't be necessary - there should only be one (at most) to iterate over – CertainPerformance Jan 08 '20 at 05:25
  • 1
    or if you want with ID then you can do `$("div#idSelector p" )` note the `#` in front of idSelector and the space then p to give you the list of all p tags in that specific div – MadaManu Jan 08 '20 at 05:27
  • If you trying to find an element "P" which is inside a div with an ID then you can use something like , ``` $("div#study_ltr p" ).get() ``` – shiva2492 Jan 08 '20 at 05:44
  • Thank you, friends, I have got my solution from comments – MNJ Jan 08 '20 at 07:08
  • You are trying to use ID but not using `#` and also need to give space between id and child element `p`. Please try like `$("#" + para_id +" " + "p")` – Sujeet Kumar Jan 08 '20 at 07:09

0 Answers0