I'm trying to figure out how to add a <wbr>
tag before punctuation in an email address, dynamically using jQuery.
I imagine there must be a way to scan the string for a "." or "@" sign and place this tag right before it, but I haven't been able to figure it out.
I've attempted two different methods which were the only solutions I was able to come up with after searching for solutions:
HTML:
<div class="container">
<span class="some-special-classname">
verylongfirstname.verylonglastname@prettylongemailaddress.com
</span>
<br /> <br />
<button class="test">Test Button</button>
</div>
CSS
wbr:after {
content:"\00200B";
}
.container {
margin: 0 auto;
max-width: 400px;
padding : 10px;
border: 1px solid #bbb;
}
Javascript: (1st. attempt)
$( ".test" ).click(function() {
$('.some-special-classname').html().replace(/@/g,"<wbr>@");
$('.some-special-classname').html().replace(/./g,"<wbr>.");
});
Javascript: (2nd. attempt)
var str = $('.some-special-classname').val();
str.replace(/@/g,'<wbr>@');
function myFunction() {
var str = $('.some-special-classname').val();
var n = str.indexOf(".");
document.getElementById("demo").innerHTML = n;
}