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

  • Home
  • SEARCH
  • 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 6913279
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:11:53+00:00 2026-05-27T09:11:53+00:00

I’m attempting to create a jQuery version of php’s wordwrap function — I asked

  • 0

I’m attempting to create a jQuery version of php’s wordwrap function — I asked an earlier question about this and was directed to this javascript function which does exactly what I’m looking for, except that I can’t get it to play friendly with jQuery — mostly I think out of ignorance.

I decided to try to adapt the code I was provided with into a jQuery plugin — for fun and because I can’t find anything to do what I want and because I figured it would be a good learning experience but I can’t get it working–

here’s the original javascript:

function wordwrap( str, width, brk, cut ) {

    brk = brk || '\n';
    width = width || 75;
    cut = cut || false;

    if (!str) { return str; }

    var regex = '.{1,' +width+ '}(\\s|$)' + (cut ? '|.{' +width+ '}|.+$' : '|\\S+?(\\s|$)');

    return str.match( RegExp(regex, 'g') ).join( brk );

}

And then my jQuery — which I think is probably all wrong:

  $.fn.wordwrap = function(options) {
    var settings = $.extend({
      'brk': '<br>',
      'width': '5',
      'cut': 'false'
    }, options);

    var regex = '.{1,' +settings.width+ '}(\\s|$)' + (settings.cut ? '|.{' +settings.width+ '}|.+$' : '|\\S+?(\\s|$)');

    return this.html(function(i, html) {
      return text.html(RegExp(regex, 'g')).join(settings.brk);
    });

I have limited experience with plugin-authoring. I’ve read through the jQuery guide at http://docs.jquery.com/Plugins/Authoring and I feel like I’m doing everything properly but it doesn’t do anything when called on an element, for example:

$('#foo').wordwrap({'width' : '25'});

I’m not getting any errors on chrome web inspector but nothing happens on my page either…

I believe the problem lies in the return string at the end of the function — I am guessing that I am not doing something properly down there in order to get the desired output

Any help greatly appreciated!

UPDATE:
GOT IT THANKS!
Here’s the final script I’m using:

  $.fn.wordwrap = function(options) {
    var settings = $.extend({
      'brk': '<br>',
      'width': '75',
      'cut': 'false'
    }, options);

    var regex = '.{1,' +settings.width+ '}(\\s|$)' + (settings.cut ? '|.{' +settings.width+ '}|.+$' : '|\\S+?(\\s|$)');

    return this.html(function(i, html) {
      var match = html.match(RegExp(regex, 'g'));
      if (match != null) {
        return match.join(settings.brk); 
      }
    });
  • 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-05-27T09:11:54+00:00Added an answer on May 27, 2026 at 9:11 am

    You don’t return text from a jQuery plugin like that, you return the collection you are working on. This makes chaining work.

    I think this is what you want…

    $.fn.wordwrap = function(options) {
        var settings = $.extend({
            'brk': '<br>',
            'width': '75',
            'cut': 'false'
        }, options);
    
        var regex = '.{1,' + settings.width + '}(\\s|$)' + (settings.cut ? '|.{' + settings.width + '}|.+$' : '|\\S+?(\\s|$)');
    
    
        return this.text(function(i, text) {
                  return text.match(RegExp(regex, 'g')).join(settings.brk);
               });
    
    };
    

    You should also change the cut option to default to false. Wrapping it in a string just makes things harder. The width option should also just be a Number.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I am reading a book about Javascript and jQuery and using one of the
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't

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.