Preserve formatting of text using letterings.js - javascript

Is there a way to preserve formatting (e.g. bold, italic) of text when using the letterings.js plugin? I am using the "word" wrapping function (https://github.com/davatron5000/Lettering.js/wiki/Wrapping-words-with-lettering%28%27words%27%29) and it seems to destroy any formatting made of the text.
Here's an example:
<div class="text-block">This is a <i>sentence</i> <b>with <i>formatting</i></b>.</div>
After using letterings.js, it turns into:
<div class="text-block">
<span class="word1">This</span>
<span class="word2">is</span>
<span class="word3">a</span>
<span class="word4">sentence</span>
<span class="word5">with</span>
<span class="word6">formatting.</span>
</div>
This is the function I'm using in jQuery:
$('.text-block').lettering('words');
I've found that I can preserve bold or italic (unfortunately not both) doing this:
$('.text-block b').lettering('words');
-OR-
$('.text-block i').lettering('words');
You can't use both ( i.e. $('.text-block b,.text-block i') ) at the same time.
If it's not possible with letterings.js, is there another plugin or method to wrap each word in spans but preserve the formatting?

If you change the method in line 15 from .text() to .html(), you can use
$('.text-block').lettering('words');
as is and it will not strip the tags.
Use this link to get the source:
https://gist.github.com/klatchbaldar/6956474

Related

Jquery append data attribute value dynamically using html

I am using jquery sortable library where i will drop item from one list to another.In the process of dropping item i will change html content.I have data attribute also.
onAdd: function (evt) {
var df="hm "+evt.item.innerText;
alert(df);
var content='<div class="list-group-item " >'+evt.item.innerText+' its modified <span class="badge badge-primary badge-pill float-right" data-job='+df+'>14</span></div>';
$(evt.item).replaceWith(content);
},
in alert i get correct appended value but in data-job it will set only hm and evt.item.innerText will not append
Can any one help me to append it properly.
Thank you
You are missing quotes in the expression
Use below code
data-job="'+df+'"
Since you have space between the text, the text after the space is treated as another attribute of the element. You should wrap the text with quotes data-job="'+df+'".
Though I prefer using Template literals which is more cleaner and easier to use:
Template literals are string literals allowing embedded expressions. You can use multi-line strings and string interpolation features with them. They were called "template strings" in prior editions of the ES2015 specification.
Demo:
function test(evt) {
var df="hm "+evt.target.innerText;
var content=`<div class="list-group-item">evt.target.innerText its modified <span class="badge badge-primary badge-pill float-right" data-job='${df}'>14</span></div>`;
console.log(df);
console.log(content);
$('body').append(content);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js"></script>
<div onclick="test(event)">test</div>

JavaScript string: get content of two standing next to each other pieces of content and wrap them together

I'm trying to create a small script that would wrap some parts of text from e.g. <p> tag like this one: <p>... 'displayed text'[popup content] ...</p> in a span wrapper.
The end result would look like this:
<span class='wrapper'>
displayed text
<span class='popup'>popup content</span>
</span>
At the moment I'm able to find and replace the text between apostrophes like this:
some_string.replace(/'(.*?)'/g,'<span>$1</span>');
But I would really like to wrap the popup content part first and then wrap it together with displayed text inside the wrapper element.
Would that be possible?
Sure - how about this?
some_string.replace(/'(.*?)'\[(.*?)\]/, "$1<span class='popup'>$2</span>");
Add a \s* between the two parts of the regex if they could be separated by whitespace:
/'(.*?)'\s*\[(.*?)\]/

Best practice when adding whitespace in JSX

I understand how (and why) to add a whitespace in JSX, but I am wondering what's best practice or if any makes any real difference?
Wrap both elements in a span
<div className="top-element-formatting">
<span>Hello </span>
<span className="second-word-formatting">World!</span>
</div>
Add them on one line
<div className="top-element-formatting">
Hello <span className="second-word-formatting">World!</span>
</div>
Add space with JS
<div className="top-element-formatting">
Hello {" "}
<span className="second-word-formatting">World!</span>
</div>
Because &nbsp causes you to have non-breaking spaces, you should only use it where necessary. In most cases, this will have unintended side effects.
Older versions of React, I believe all those before v14, would automatically insert <span> </span> when you had a newline inside of a tag.
While they no longer do this, that's a safe way to handle this in your own code. Unless you have styling that specifically targets span (bad practice in general), then this is the safest route.
Per your example, you can put them on a single line together as it's pretty short. In longer-line scenarios, this is how you should probably do it:
<div className="top-element-formatting">
Hello <span className="second-word-formatting">World!</span>
<span> </span>
So much more text in this box that it really needs to be on another line.
</div>
This method is also safe against auto-trimming text editors.
The other method is using {' '} which doesn't insert random HTML tags. This could be more useful when styling, highlighting elements, and removes clutter from the DOM. If you don't need backwards compatibility with React v14 or earlier, this should be your preferred method.
<div className="top-element-formatting">
Hello <span className="second-word-formatting">World!</span>
{' '}
So much more text in this box that it really needs to be on another line.
</div>
You can use the css property white-space and set it to pre-wrap to the enclosing div element.
div {
white-space: pre-wrap;
}
I tend to use
It's not pretty but it's the least confusing way to add whitespace I've found and it gives me absolute control over how much whitespace I add.
If I want to add 5 spaces:
Hello <span className="second-word-formatting">World!</span>
It's easy to identify exactly what I'm trying to do here when I come back to the code weeks later.
You can add simple white space with quotes sign: {" "}
Also you can use template literals, which allow to insert, embedd expressions (code inside curly braces):
`${2 * a + b}.?!=-` // Notice this sign " ` ",its not normal quotes.
You can use curly braces like expression with both double quotes and single quotes for space i.e.,
{" "} or {' '}
You can also use ES6 template literals i.e.,
` <li></li>` or ` ${value}`
You can also use &nbsp like below (inside span)
<span>sample text </span>
You can also use &nbsp in dangerouslySetInnerHTML when printing html content
<div dangerouslySetInnerHTML={{__html: 'sample html text: '}} />
I have been trying to think of a good convention to use when placing text next to components on different lines, and found a couple good options:
<p>
Hello {
<span>World</span>
}!
</p>
or
<p>
Hello {}
<span>World</span>
{} again!
</p>
Each of these produces clean html without additional or other extraneous markup. It creates fewer text nodes than using {' '}, and allows using of html entities where {' hello & goodbye '} does not.
You don't need to insert or wrap your extra-space with <span/>. Just use HTML entity code for space -
Insert regular space as HTML-entity
<form>
<div>Full name:</span>
<span>{this.props.fullName}</span>
</form>
use {} or {``} or to create space between span element and content.
<b> {notif.name} </b> <span id="value"> { notif.count }{``} </span>
Despite using this is a neat approach: using the <Fragment> tag to insert HTML inside a variable, which allows the creation of a custom spacer to be used in JSX.
Import { Fragment } ...
import { Fragment } from "react"
Create the variable..
const space = <Fragment> </Fragment>
Note: it can also be done with <span> instead of <Fragment>
Use like so..
return (
<div> some text here {space} and here... </div>
)
If the goal is to seperate two elements, you can use CSS like below:
A<span style={{paddingLeft: '20px'}}>B</span>

How do I find a specific child element using javascript

I've just started using javascript and am trying to do the following:
My html doc contains divs and span elements like below.
I want to create a variable which will store the value of my-new-id for a specific div child element.
The only information I have to go on, is that I always know what the span text will be cause it's based on a username.
Therefore, how would I get the value of my-new-id for user1?
I have already figured out how to navigate to the correct div elemenet using the document.getElementById method. I then try to use a jquery selector such as :contains, but get stuck.
Many thanks on advance.
<div id=1>
<span my-new-id=x>user1</span>
<span my-new-id=y>user2</span>
<span my-new-id=z>user3</span>
</div>
<div id=2>
<span my-new-id=x>user10</span>
<span my-new-id=y>user1</span>
<span my-new-id=z>user30</span>
</div>
Using jQuery:
var val = $('span:contains("user1")').attr('my-new-id');
A couple of additional points:
Do not use IDs that begin with a number. This is not allowed in the HTML4 spec, and can cause unexpected behavior in some browsers. Instead, prefix your ID's with an alphabetic string, like this:
<div id="container1">
...
</div>
I would recommend that you use data-* attributes instead of making up non existent attributes. You can make data attributes like this:
<span data-new-id="x">user1</span>
You can then retrieve this value using:
$('span:contains("user1")').data('newId');
Note that the attribute name has been stripped of dashes and camel-cased.

Getting element inner text ONLY when an <img /> tag exists inside using jQuery 's html()

I'm trying to get the html inside several elements like this:
<div class="option s12 pointer"><img class="fl mt2" src="http://web.iboomerang.com/icons/fff/magnifier.png" /> Trainings by date</div>
Is there a way to strip the tag (like PHP's strip_tags does) and leave only "Trainings by date".
Note: I need to avoid using functions like split() or replace(), because of how versatile this app needs to be.
I think you want this: http://docs.jquery.com/Attributes/text
You can use jQuery's text() function:
var text = $('div.option').text();
You can easily replace 'div.option' with whatever CSS selector(s) you need.

Categories

Resources