-1

I have a kinda data as like below when I select the p tag using its classname it returns <strong>Packages:</strong> but i want the data to be selected as like this <p class="first"><strong>Packages:</strong> </p>.

alert($('.mydiv').html());
$('.itemize div p:first').css('background-color','red');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="itemize">
    <p> Order Summery</p>
    <div class="mydiv">
    <p class="first"><strong>Packages:</strong> </p> 
    </div>
   </div>

Can somebody help me to get the whole p tag including <strong> element. Any suggestions, Please !

06011991
  • 797
  • 2
  • 13
  • 39

3 Answers3

1

You have to use OuterHTML , OuterHTML return selector html code.

alert($('.itemize div p.first')[0].outerHTML);
$('.itemize div p:first').css('background-color','red');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="itemize">
    <p> Order Summery</p>
    <div>
    <p class="first"><strong>Packages:</strong> </p> 
    </div>
   </div>
chirag satapara
  • 1,947
  • 1
  • 15
  • 26
1

alert($('.itemize div p.first').get(0).outerHTML);
alert($('.itemize div p.first').prop('outerHTML'));
$('.itemize div p:first').css('background-color','red');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="itemize">
    <p> Order Summery</p>
    <div>
    <p class="first"><strong>Packages:</strong> </p> 
    </div>
   </div>
mrid
  • 5,782
  • 5
  • 28
  • 71
0

Change your jquery code like this with :last :

alert($('.itemize div p:last').html());
$('.itemize div p:last').css('background-color','red');
Himanshu Upadhyay
  • 6,558
  • 1
  • 20
  • 33