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

Get each line of text element

Wrap element to new line/row using flexbox

Use jQuery to wrap text after element

Wrap input to value text length using each

Using Jquery wrap function with absolute placed element

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

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

Wrap each line with li

Append each line of a text as a element of a nested list

How to wrap selected text with html element without losing line breaks?

Wrap text in span element

jQuery for each input change text for matching element

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

Using sed to insert text at the beginning of each line

how to wrap each input and label in one div using jquery

How to add a different title (Hover-Text) for each value of a slider element without using JQuery?

jQuery - text deleted by .html() when targeting element directly but not when using .each()

Wrap each line of paragraph in a span

Wrap specific field on each line

Add the text to the second line using pseudo element

Replace a single letter wrap with span element using jquery

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

Jquery - using each() to get position of a class element

element is not defined using for each loop in jquery

Set Display of an element using .each() in jQuery

Wrap text in span after string - using jquery/javascript

Wrap a particular text string inside an HTML tag with a <span> using jQuery

How to wrap sibling text of elements with tag using javascript/jquery?

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