2

Possible Duplicate:
Jquery or javascript to add one line break <br /> after x amount of characters in a <div>

I'd like to insert a br tag five characters into the content of the element. For example, from this:

<p>qwertyuiopasdfghjklzxcvbnm</p>

To this:

<p>qwert<br />yuiopasdfghjklzxcvbnm</p>

How can I do this with jQuery?

Community
  • 1
  • 1
ruslyrossi
  • 366
  • 1
  • 6
  • 21
  • 2
    similar question http://stackoverflow.com/questions/7068653/jquery-or-javascript-to-add-one-line-break-br-after-x-amount-of-characters-i – karth May 19 '12 at 06:24
  • the problem is i want to add
    because i work with languange like japan, if in English we have sentence or word but in japan no space between word or sentence that why i want to add something like
    – ruslyrossi May 19 '12 at 06:27

2 Answers2

1

Simply replace the innerHTML with this function (Fires on document.ready()):

$(document).ready(function() {
    $('p').html($('p').html().substring(0,5)+'<br/>'+$('p').html().substring(5));
}
);

You can try it on jfiddle: http://jsfiddle.net/KwpGr/1/

int2000
  • 565
  • 3
  • 14
0

You could use an implicit callback on $.html():

/* Be sure to specify which paragraph this applies to */
$("p").html(function(i,v){
  return v.replace( /^([\w]{5})/, "$1<br/>" );
});

Demo: http://jsbin.com/orovom/2/edit

Sampson
  • 265,109
  • 74
  • 539
  • 565