Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8717205
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T06:15:22+00:00 2026-06-13T06:15:22+00:00

I am trying to insert a <br /> after 5 words in a title,

  • 0

I am trying to insert a <br /> after 5 words in a title, but the piece of code I’m using doesn’t work. Please see below:

$("#post-id > h2").each(function() {
    var html = $(this).html().split(" ");
    html = html[0] + "<br />" + html.slice(1).join(" ");
    $(this).html(html);
});

This function works well as it is – if you want to add a <br /> tage after the first word. But if I change it to html.slice(5) then it doesn’t work properly.

How could I fix this so it works fine after 5 words? Or maybe a different approach would be more appropriate? Thanks for your help in advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T06:15:23+00:00Added an answer on June 13, 2026 at 6:15 am
    $("#post-id > h2").each(function() {
         var html = $(this).html().split(" ");
         html = html.slice(0,5).join(" ") + "<br />" + html.slice(5).join(" ");
         $(this).html(html);
    });​
    

    http://jsfiddle.net/f4chL/

    Basic idea is you take the first 5 words via slicing from index zero, and getting 5 elements of the array, join them, add your break tag, then get the remaining elements in the array at index 5 (as its a zero based index) and join them with spaces and dump it on the end

    edit

    for the sake of a full answer and if the OP does want to split after every 5 words, here’s another method

    $("h2").each(function() {
    var html = $(this).html().split(" ");
    var newhtml = [];
    
    for(var i=0; i< html.length; i++) {
    
        if(i>0 && (i%5) == 0)
            newhtml.push("<br />");
    
        newhtml.push(html[i]);
    }
    
    $(this).html(newhtml.join(" "));
    });​
    

    http://jsfiddle.net/2Z2nM/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to insert something into a local database, but it doesn't work.. ModelContainer
I am trying to insert a <br clear=all /> after each fifth div. Below
I am trying to insert into mysql from an xml using PHP, but the
I have been trying to work out how to insert text before and after
I'm trying to insert a link after form elements to clear them. This demo
I'm trying to insert a space after each semi-colon, unless the semi-colon is part
I'm trying to insert a string that begins with the word title as in
My trigger is as below , Alter TRIGGER [dbo].[LogTable_InsertTrigger] on [dbo].[std_table] AFTER INSERT as
I am trying to restore a DB using an SQL script, but things foreign
i was trying to see if there's already a similar question but couldn't find

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.