Boostr



0 votes
6 views
I want to replace second space occurrence of the sentence with a br.

I have tried this but it is deleting the rest.

var title = "My Title Needs Brace".split(" ").slice(0, 2).join(" ");
asked Dec 30, 2017 in JAVASCRIPT by Amrendra | 6 views



2 Answers

0 votes
Best answer
Try the following:

var title = "My Title Needs Brace".split(" ");

title.forEach(function(item, i, title){
  if(i==1)
    title[i] += "<br/>";
  else
  title[i] += ' ';
})

console.log(title.join(''));
answered Dec 30, 2017 by Amrendra
0 votes
want to replace second space occurrence of the sentence with a br.

The simple way to do that is to add "<br/>" to the second element.

Here is the Code.

$(document).ready(function(){
  var title = "My Title Needs Brace".split(" ");
  title[1] = title[1]+"<br/>";
  var newstr = title.join(" ");
  $("#textd").html(newstr);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="textd">
</div>
answered Dec 30, 2017 by Amrendra

Related Questions



Welcome to Knowledge Boostr, where you can ask questions and receive answers from other members of the community.
This is a collaboratively edited question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

http://boostr.in Q&A for professional and enthusiast programmers http://boostr.in Q&A for professional and enthusiast programmers http://boostr.in Q&A for professional and enthusiast programmers http://boostr.in Q&A for professional and enthusiast programmers


23,494 questions
40,169 answers
12 comments
23,610 users