Reputation: 435
Currently, the string is
Package A (123 queries) Package B (212 queries)
Is it possible to use jquery to split it into new line like
- Package A (123 queries)
- Package B (212 queries)
I was thinking to insert line break after each closing bracket. But not sure if it is appropriate.
Edit 1
In HTML
<ul class="list">
<li><span id="requested-package">@Model[0].regDescription[a]</span></li>
<li>Testing</li>
</ul>
Output - I want to make the Plan A in next line
Edit 2
var str = $('.hidden-package').text();
var line = str.replace(')', ') <br>');
$('.requested-package').html(line);
Current Output
I created a hidden field hidden-package
and from there I managed to get the text value using .text()
.
The problem now is it only replaces the first occurrence of closing bracket. How can I replace all the closing bracket with <br>
. Thanks.
Solution
Managed to solve this by changing the code above to the below.
var str = $('.hidden-package').text();
var line = str.replace(/\)/g, ')<br>');
$('.requested-package').html(line);
Upvotes: 2
Views: 3347
Reputation: 211
I needed to convert contents of the variable someStr
to string using toString()
function:
So if my string looks like this:
someStr = 'Package A (123 queries)~ Package B (212 queries)'
with the special character "~" inserted (as suggested by @user2703788), convert the variable someStr
as:
someStr = someStr.toString();
And then replace
So we may use:
someStr = someStr.toString().replace(/~/g, "<br />");
And display the contents:
$('#myMessage').html(someStr);
Upvotes: 0
Reputation: 163
Add any special character where you want to split the string and replace it with a line break, this should add a new line in html.
For example
var string = "Package A (123 queries)~Package B (212 queries)~Package C (212 queries)";
string = string .replace(/~/g, "<br />");
Upvotes: 1
Reputation: 362
Use .replace
but try to be as specific as possible. Here we look for where you have a closing bracket followed by a space followed by 'Package' followed by another space ') Package '
. This should help prevent erroneous results
'Package A (123 queries) Package B (212 queries)'.replace(') Package ', ')\nPackage')
Upvotes: 3
Reputation: 166
'Package A (123 queries) Package B (212 queries)'.replace(') ', ')\n')
Upvotes: 2