jQuery select and wrap textNode
Date : March 29 2020, 07:55 AM
I hope this helps you . You can use contents, and filter by node type (3 is for text node): $('div').contents()
.filter(function(){return this.nodeType === 3})
.wrap('<b />');
|
Wrap unwrapped parts of HTML string with Javascript
Date : March 29 2020, 07:55 AM
will be helpful for those in need I have string in variable (Javascript/jQuery) containing content like this: , Something like var str = 'your string';
var div = $('<div />', {html: str});
div.contents().filter(function() {
return this.nodeType === 3;
}).wrap('<p />');
var new_str = div.html();
|
Vaadin - Wrap arbitrary Component with HTML
Date : March 29 2020, 07:55 AM
I hope this helps . You can use a CustomLayout to put components into arbitrary HTML surroundings (See also the Book of Vaadin)Creating that HTML is not a direct part of Vaadin. So you have to pick some third party library for this. For the sake of example is used Jsoup, as this is included in the Vaadin server and it allows for DOM manipulations (which I don't use): // run with `spring run --watch <file>.groovy`
@Grab('com.vaadin:vaadin-spring-boot-starter:2.0.1')
import com.vaadin.ui.*
import com.vaadin.ui.themes.*
import com.vaadin.shared.*
@com.vaadin.spring.annotation.SpringUI
@com.vaadin.annotations.Theme("valo")
class MyUI extends UI {
protected void init(com.vaadin.server.VaadinRequest request) {
// create some HTML and provide a "place-holder" via `data-location`
def doc = org.jsoup.Jsoup.parse('<main><h1>Hello World</h1><p><span data-location="button"/></p></main>')
def button = new Button("Hello World", { Notification.show("Hello World") } as Button.ClickListener)
// read the CustomLayout from an InputStream
def c = new CustomLayout(new ByteArrayInputStream(doc.toString().getBytes('UTF-8')))
// place the button in the CustomLayout via its name chosen by `data-location`
c.addComponent(button, 'button')
content=c
}
}
|
How to wrap unwrapped strings in html tag?
Date : March 29 2020, 07:55 AM
help you fix your problem Create an element, put that string into the element and then iterate the child nodes and replace text nodes with new that contains associated text. Then finally get the resultant html // From
var first = `Text here <code>var first = "first";</code> Another text
<code>var second = "second";</code>`
var div = document.createElement('div');
div.innerHTML = first;
var nodes = div.childNodes;
for (var i = 0; i < nodes.length; i++) {
if (nodes[i].nodeType == 3) {
var p = document.createElement('p');
p.textContent = nodes[i].textContent;
div.replaceChild(p, nodes[i])
}
}
let res = div.innerHTML;
console.log(res)
|
Wrap unwrapped textNode using jQuery
Date : December 05 2020, 12:08 PM
hop of those help? I have a HTML structure like this: , Using html() and contents() $('.col-md-6').html(function() {
return $('<p/>').html($(this).contents())
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="col-md-6">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod <a href="#">tempor</a> incididunt ut labore et dolore magna aliqua.</div>
$('.col-md-6').each(function() {
$(this).contents().filter(function() {
return this.nodeType === 3;
}).siblings().addBack().wrapAll('<p></p>')
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="col-md-6">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod <a href="#">tempor</a> incididunt ut labore et dolore magna aliqua.</div>
<div class="col-md-6"><p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod <a href="#">tempor</a> incididunt ut labore et dolore magna aliqua.</p></div>
<div class="col-md-6">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod <a href="#">tempor</a> incididunt ut labore et dolore magna aliqua.</div>
|