score:4
Accepted answer
this
refers to a plain DOM node element that doesn't implement neither an html()
nor a text()
method. Using $(this)
, you can make the element into a jQuery collection in order to be able to access the jQuery methods. Then you can use replaceWith()
to replace the plain text nodes with the <div>
s.
$('div').contents().filter(function()
{
return this.nodeType === 3;
}).each(function()
{
$(this).replaceWith("<div>" + $(this).text() + "</div>");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
aaaa
<span>bbbb</span>
cccc
<span>dddd</span>
eeee
</div>
score:1
You can also use wrap
from jquery to wrap the content with div
.wrap()
Description: Wrap an HTML structure around each element in the set of matched elements.
$('div').contents().filter(function()
{
return this.nodeType === 3;
}).each(function()
{
$(this).wrap('<div>');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
aaaa
<span>bbbb</span>
cccc
<span>dddd</span>
eeee
</div>
Related Query
- Convert text node into HTML element using JQuery
- How would I convert HTML into text using jQuery?
- Determine if element is a text node using jQuery
- Calculate width of text node in a block level element using jQuery
- Send/copy text within one HTML element to another using jquery
- Find all elements on a page whose element ID contains a certain text using jQuery
- Convert jquery element to html element
- How do I replace the entire HTML node using jQuery
- Get the text after span element using jquery
- How do I hide an HTML element before the page loads using jQuery
- Replacing / Manipulating element in html string using jquery
- Convert a HTML table data into a JSON object in jQuery
- remove parent element but keep the child element using jquery in HTML
- How to remove text (without removing inner elements) from a parent element using jquery
- Convert HTML string into Jquery Object
- how to convert total html page to pdf using javascript or jquery
- Convert string into jQuery object and select inner element
- Is there a way to convert HTML into normal text without actually write it to a selector with Jquery?
- Convert a string of HTML into DOM objects with jQuery
- Setting the text of an <option> element using jQuery
- How to load html using jquery into a TinyMCE textarea
- How to highlight a part part of an Input text field in HTML using Javascript or JQuery
- how to make focus on the html element using jquery
- Extracting entire column of data from HTML table into array using jQuery
- Find a child element with specific text (value) using jQuery
- I get NaN when I try to insert some HTML into a DIV element with jQuery
- Which event belongs to "type text into textfield" using jQuery
- Remove element and insert its text into parent element in the same place with jQuery
- Inserting HTML into DIV using JQuery
- Is there an easy way to convert text into HTML in JavaScript?
- move an element from one div to another div with animate effect on button click using html and jquery
- Convert a jQuery set into HTML
- How to disable jquery validation on keyup and focusout for 1 specific html element when using the unobtrusive validation plugin?
- Accessing an array of HTML input text boxes using jQuery or plain Javascript
- How to select own text of the element using jQuery
- using casperjs to click an html element based on its text
- jQuery UI remove element when dropped into a div using .droppable
- jquery - find element that only has text and not any other html tag
- jQuery plugin to fit text into a width/height defined element
- Get HTML element using jQuery
- Convert HTML table to ul li using jquery
- How do I convert an HTML Entity Number into a character using plain JavaScript or jQuery?
- How to clear the content of the html input text field using Jquery
- How to convert all items (options) of a multi select dropdown into an array using jquery
- How do I Get Plain Text Beside an HTML Element using jQuery?
- jQuery update html element text without affect the HTML children elements
- How can I convert <br> string into actual HTML <br> using jQuery?
- How to make html text box draggable and resizable using jQuery UI plugins?
- Querying for script elements from HTML text using jQuery
- Can't convert markdown into HTML using StackExchanges's PageDown
More Query from same tag
- jQuery: how do I get the element underneath my cursor?
- $.each() with nested array
- Chrome does not recognize height for SELECT option (Both jQuery & CSS)
- jQuery's .load() not working in IE - but fine in Firefox, Chrome and Safari
- Create a Leaflet custom checkbox control
- Searchable/filterable treeview
- How can we create a custom jquery plugin using typescript?
- Equivalent of jQuery.active in javascript Selenium Web Driver
- jQuery 1.9 - Internet Explorer 9 freezes after first $ajax request
- Header that stays fixed in place as navigating through pages (it is never reloaded)
- Jquery clone original HTML not DOM
- multiple select js when click on single element raise also checkall
- change text color depending on background image / color
- How to get label values of checkbox in javascript
- Get Raw HTML of Node in JQuery
- If statement involving this.indexof in order to animate an element not working
- Getting the parent from the matched element in jQuery
- How to get the attribute's value from outerHTML's text?
- Get first and last date of current month with JavaScript or jQuery
- Javascript get img src and show link in a div
- Waiting for data from async nested Functions within JQuery $.each in Javascript
- Is it possible to suppress parent's :active pseudo class?
- File Upload Progress Bar with Multiple and Different Inputs(MVC)
- JavaScript Date.toString() radix argument error
- How to toggle display on content using multiple Bootstrap 4 display classes in concert
- jQuery AJAX call results in error status 403
- Move and remove Listbox item to another listbox using jQuery in same places
- how to send a collection/array to mvc action via ajax
- Typewriter Effect with jQuery
- Get contents of a div from a URL