Split and insert <br />

by someprimetime

HTML

<a href="#" id="alt" alt="You need to login or register to do that bro ok?">yo</a>
<div id="output"></div>

JavaScript

$(function() {
    var $alt = $('#alt').attr('alt'); // Count number of spaces so we know where to split the string    
    $spaceCount = $alt.split(' ');
    $spaceCount = $spaceCount.length;
    // We floor the number because we want the bottom row to have more text (generally)    
    $split1 = Math.floor($spaceCount / 2);
    $split2 = $spaceCount - $split1;
    $alt = $alt.split(' ');
    var firstHalf = [];
    
    for (var i = 0; i < $split1; i++) {
        firstHalf.push($alt[i]);
    }

    var secondHalf = $alt.splice($split1, $split2);

    var splitString = (firstHalf + '<br />' + secondHalf);
    splitString = splitString.split(',').join(' ');
    
    $('#output').html(splitString);

});