How to add div to text within dynamic nested div using jquery

67
May 19, 2017, at 7:03 PM

Below is the HTML format code for the nested div

<div>
   <div>
         Text 1
        <div>
            Text 2
        </div>
   </div>
</div

I want to make this like below using jquery or javascript

    <div>
       <div>
             <span class="custom">Text 1</span>
            <div>
                <span class="custom">Text 2</span>
            </div>
       </div>
    </div
Answer 1

$('div').contents().filter(function() { 
  return this.nodeType == 3; 
}).wrap("<span class=custom></span>")
.custom { 
  color: red 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
<div> 
  <div> 
    Text 1 
    <div> 
      Text 2 
    </div> 
  </div> 
</div>

  1. Use node 3 as anchor since it is text.
  2. use .wrap() to wrap text in span
Answer 2
$([your element selector here]).contents()
                               // get all text nodes
                               .filter(function(){return this.nodeType === 3})
                               // wrap in a span element
                               .wrap('<span />')
                               // add class custom to each element
                               .addClass('custom');
READ ALSO
Ember component multiple ajax calls

Ember component multiple ajax calls

I am very new to ember js 213

113
How to open new form on dynamic button click event in php

How to open new form on dynamic button click event in php

I have created dynamic button in phpi need to to open new form in same homepage on that dyanamic button click event and when i am clicking on button it refresh the page

92
ASP MVC Refresh partial view grid

ASP MVC Refresh partial view grid

I am trying to refresh a partial view grid after changing a recordI have a button where the user can click on, than it will change a row in the grid

77
jQuery AJAX function call

jQuery AJAX function call

I have a problem with jQuery calling an AJAX function, basically everytime a user changes a select box, I want it to call the getSubCategories function, but for some reason, nothing is happeningAny ideas?

88