Wrap each line text to an element using JQuery

MCN

I have this

<div class="mad-select">
<ul class="madSelect">
Hours
Days
Weeks
Months
</ul></div>

Then i want to make it like this by using JQuery

<div class="mad-select">
<ul class="madSelect">
    <li>Hours</li>
    <li>Days</li>
    <li>Weeks</li>
    <li>Months</li>
</ul></div>

How can i do this?

Bla...

You can first access the text inside <ul> then split them. After that just clear anything in <ul> then add the <li>.

$(document).ready(function(){

var arr = $('.madSelect').html().split('\n');
$('.madSelect').empty();

$.each(arr, function( index, value ) {
  if ( index > 0 && index < arr.length - 1) 
    $('.madSelect').append("<li>"+arr[index]+"</li>");
});



});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="mad-select">
<ul class="madSelect">
Hours
Days
Weeks
Months
</ul></div>

EDIT:

I changed from .text() to .html(), because .html() is ~2x faster than .text()if you have only a text.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Wrap text inside of an element with jQuery

From Dev

Wrap substring of element content with an element using jQuery

From Dev

Wrap element to new line/row using flexbox

From Dev

Use jQuery to wrap text after element

From Dev

PERL : Using Text::Wrap and specify the end of line

From Dev

Using Jquery wrap function with absolute placed element

From Dev

JQuery if line of text starts with character, wrap whole line in span

From Dev

How to loop text file each line and append other text wrap each line

From Dev

How to loop text file each line and append other text wrap each line

From Dev

Wrap each line in a text file in apostrophes and add comma to end of lines

From Dev

How to wrap HTML text in each new line with <li> tag?

From Dev

Wrap each line with li

From Dev

Wrap each line with li

From Dev

JQuery collect group of elements, unwrap each and then wrap all in new element

From Dev

How to wrap a string using element from the list using jquery

From Dev

Wrap each line of a paragraph in a span

From Dev

Wrap each line of paragraph in a span

From Dev

Wrap specific field on each line

From Dev

Replace a single letter wrap with span element using jquery

From Dev

Find parent of unwrapped element and use it in .wrap() using jquery

From Dev

Find parent of unwrapped element and use it in .wrap() using jquery

From Dev

Getting element values in jQuery using each

From Dev

Printing each text element on new line after tab indent in r

From Dev

Inserting text at begining of each line of text file using shell script

From Dev

wrap element around span text

From Dev

Wrap text within element LXML

From Dev

jQuery each: If element is in the element

From Dev

How to wrap an element with a div in jQuery?

From Dev

Wrap element after image with jquery

Related Related

  1. 1

    Wrap text inside of an element with jQuery

  2. 2

    Wrap substring of element content with an element using jQuery

  3. 3

    Wrap element to new line/row using flexbox

  4. 4

    Use jQuery to wrap text after element

  5. 5

    PERL : Using Text::Wrap and specify the end of line

  6. 6

    Using Jquery wrap function with absolute placed element

  7. 7

    JQuery if line of text starts with character, wrap whole line in span

  8. 8

    How to loop text file each line and append other text wrap each line

  9. 9

    How to loop text file each line and append other text wrap each line

  10. 10

    Wrap each line in a text file in apostrophes and add comma to end of lines

  11. 11

    How to wrap HTML text in each new line with <li> tag?

  12. 12

    Wrap each line with li

  13. 13

    Wrap each line with li

  14. 14

    JQuery collect group of elements, unwrap each and then wrap all in new element

  15. 15

    How to wrap a string using element from the list using jquery

  16. 16

    Wrap each line of a paragraph in a span

  17. 17

    Wrap each line of paragraph in a span

  18. 18

    Wrap specific field on each line

  19. 19

    Replace a single letter wrap with span element using jquery

  20. 20

    Find parent of unwrapped element and use it in .wrap() using jquery

  21. 21

    Find parent of unwrapped element and use it in .wrap() using jquery

  22. 22

    Getting element values in jQuery using each

  23. 23

    Printing each text element on new line after tab indent in r

  24. 24

    Inserting text at begining of each line of text file using shell script

  25. 25

    wrap element around span text

  26. 26

    Wrap text within element LXML

  27. 27

    jQuery each: If element is in the element

  28. 28

    How to wrap an element with a div in jQuery?

  29. 29

    Wrap element after image with jquery

HotTag

Archive