I have the following span element:
<div class="text" contenteditable="true" id="example">
<div class="outside">Type here <span class="inside"> please</span>.</div>
<div class="outside">Name here <span class="inside"> first </span>name</div>
</div>
Using the following js function, I can get the length of in between </span>
and </div>
:
$('span.inside').each(function() {
var sphs = $(this).get(0).nextSibling;
var len = sphs.length;
//If the length is less than 2
if(len < 2){
// i want to replace it with another words like "thanks."
}
})
If the length is less than 8, then I want to replace it with another words or strings ( for example, thanks.
) IN BETWEEN </span>
and </div>
(see the word "thanks" placement).
<div class="text" contenteditable="true" id="example">
<div class="outside">Type here <span class="inside"> please</span>thanks.</div>
</div>
How would I replace it with another string?
EDIT:
Note that I am using .each
as there could be multiple span element with the same class name. In the first line, there is only a period (</span>.</div>
), thus it satisfies the condition and will be replaced with another word (in this case "thanks").
In the second line (</span>name</div>
), the length is larger than 2, thus it won't get replaced.
I hope this clarifies it.
<div class="text" contenteditable="true" id="example">
<div class="outside">Type here <span class="inside"> please</span>.</div>
<div class="outside">Name here <span class="inside"> first </span>name</div>
</div>
...will become...
<div class="text" contenteditable="true" id="example">
<div class="outside">Type here <span class="inside"> please</span> thanks.</div>
<div class="outside">Name here <span class="inside"> first </span>name</div>
</div>
Or try here https://jsfiddle.net/8pa4akzh/3/