logo
down
shadow

Wrap unwrapped textNode using regex in arbitrary HTML structure


Wrap unwrapped textNode using regex in arbitrary HTML structure

Content Index :

Wrap unwrapped textNode using regex in arbitrary HTML structure
Tag : javascript , By : richardD
Date : November 29 2020, 12:01 PM

seems to work fine Since your structure is fixed it's just a matter of
Finding the first non-empty TextNode Creating a p element Substituting the text node with the paragraph inside the parent node.
function wrap() {
  let div = document.querySelector('div.row>div.col-md-12');    // get the container
  for (let i = 0; i < div.childNodes.length; i++) {             // loop through children
    let n = div.childNodes[i];
    if (n.nodeName == "#text" && n.textContent.trim() !== '') { // children found!
      let p = document.createElement('p');                      // create a `p` element
      p.textContent = n.textContent;                            // put the original text inside
      n.parentNode.replaceChild(p, n);                          // swap!
      break;                                                    // we're done here
    }
  }
}
p {
  background-color: green;
}
<div class="row">
  <div class="col-md-12">
    <h2>Some Title</h2>
    Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>
  </div>
</div>
<button onclick="javascript:wrap()">Wrap</button>

Comments
No Comments Right Now !

Boards Message :
You Must Login Or Sign Up to Add Your Comments .

Share : facebook icon twitter icon

jQuery select and wrap textNode


Tag : javascript , By : nobodyzzz
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


Tag : javascript , By : bdurbin
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


Tag : development , By : Killercode
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?


Tag : javascript , By : Hugo
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


Tag : javascript , By : Michael Gunderson
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>
Related Posts Related QUESTIONS :
  • Idle event on mouse move - how to stop script permanently on mouse move
  • Why do I get logic is not defined in this code
  • When I enter a number in the input, the first function starts
  • ES6 imports and node_modules hell
  • How to get css selector of clicked-on element?
  • How to auto input MySQL data (input column 1 data to auto input column 2 data with same column) in php?
  • Appending to Datalist Via Javascript
  • Customizing Material-UI Components with Styled-Components
  • Populate elements with class name using Javascript
  • How to execute promises in batches (first resolving 10, then next 10 and so on)
  • Maximo JS automation script: "importPackage" is not defined
  • How to surround selected text inside textarea with <em> on key combination CTRL+i?
  • Your configured registry does not support audit requests ~ On npm audit
  • What's the Angular equivalent of Vue <slot/>?
  • Iconify icon not updating when "data-icon" set with setAttribute()
  • Javascript newbie stuck with XMLHttpRequest return data
  • How to add obj to to state in my example Redux React
  • Loading "grunt-karma.js" tasks...ERROR >> SyntaxError: Unexpected identifier
  • Why do I get log is not defined
  • get the multiple index of the same string
  • How can I iterate over an Array object by using For IN or FOR OF
  • Launch local executable from web browser
  • How to prepare array of json objects to d3 hierarchical tree structure
  • VueJS - event emitted - show HTML before other calculations
  • Export SASS/SCSS variables to Javascript without exporting them to CSS
  • How to attach an event handler only once continuously
  • How to make button open a link in new tab in JavaScript?
  • Find range from the javascript object
  • How can I use my jQuery code as JavaScript?
  • How to show 5 images in a 2 row slider with bxslider
  • How to convert stream into variables in typescript
  • Three.js custom shader error on Chrome GL_INVALID_OPERATION: Active draw buffers with missing fragment shader outputs
  • My JavaScript doesn't work. How can I solve this problem?
  • Why is this async function is running synchronously?
  • Get updated variable from within react hook before return
  • I want to show all image preview but whenever I upload a new one the old one is gone
  • Would I be able to give access to specific electron APIs safely?
  • Why several clicks fire for each instance of a Class?
  • cant grab value from div using attr()
  • Make web service not accessible by browser?
  • Select DOM elements within template
  • How to customize angular ui-grid row background color?
  • Binding data in two respective $scope in AngularJS, how?
  • react put some props after import class
  • Variable is distributed among same components
  • switch class in jQuery/JavaScript for active/inactive state
  • How To Track Outbound Clicks Using Javascript
  • How to get total seconds elapsed since the beginning of the month?
  • Protractor: Stale element reference at "browser.wait(EC.visibilityOf(confDial), FIFTY_SECONDS);"
  • Access to an array inside an object
  • Reseting an JavaScript Interval with different value?
  • Learning Angular for the first time - version 2 or 4?
  • Rendering RSS feed (XML) in React with jquery, why do object values parsed from the feed seem empty?
  • Node.js xml2js http.request tag matching
  • applying texture to custom plane geometry in Threejs
  • How do I pass an Event as a function parameter in HTML written inside JavaScript variable?
  • Interface of object parameter with default value and default property
  • Does using try / catch rather than `.catch` observable operator with XHR requests reduce performance?
  • In IE11 background colors not working on printing
  • TypeScript interface signature "(): string"
  • shadow
    Privacy Policy - Terms - Contact Us © scrbit.com