What is Join() in jQuery?

JqueryJquery Selectors

Jquery Problem Overview


What is Join() in jquery? for example:

var newText = $("p").text().split(" ").join("</span> <span>"); 

Jquery Solutions


Solution 1 - Jquery

That's not a jQuery function - it's the regular Array.join function.

It converts an array to a string, putting the argument between each element.

Solution 2 - Jquery

You would probably use your example like this

var newText = "<span>" + $("p").text().split(" ").join("</span> <span>") + "</span>";

This will put span tags around all the words in you paragraphs, turning

<p>Test is a demo.</p>

into

<p><span>Test</span> <span>is</span> <span>a</span> <span>demo.</span></p>

I do not know what the practical use of this could be.

Solution 3 - Jquery

The practical use of this construct? It is a javascript replaceAll() on strings.

var s = 'stackoverflow_is_cool';  
s = s.split('_').join(' ');  
console.log(s);

will output:

stackoverflow is cool

Solution 4 - Jquery

join is not a jQuery function .Its a javascript function.

The join() method joins the elements of an array into a string, and returns the string.The elements will be separated by a specified separator. The default separator is comma (,).

http://www.w3schools.com/jsref/jsref_join.asp

Solution 5 - Jquery

I use join to separate the word in array with "and, or , / , &"

EXAMPLE

HTML

<p>London Mexico Canada</p>
<div></div>

JS

 newText = $("p").text().split(" ").join(" or ");
 $('div').text(newText);

Results

London or Mexico or Canada

Solution 6 - Jquery

A practical example using a jQuery example might be

 var today = new Date();
 $('#'+[today.getMonth()+1, today.getDate(), today.getFullYear()].join("_")).whatever();

I do that in a calendar tool that I am using, this way on the page load, I can do certain things with today's date.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmSafdelView Question on Stackoverflow
Solution 1 - JqueryGregView Answer on Stackoverflow
Solution 2 - JqueryJan AagaardView Answer on Stackoverflow
Solution 3 - JqueryDel PedroView Answer on Stackoverflow
Solution 4 - JqueryChandan GorapalliView Answer on Stackoverflow
Solution 5 - JquerySodhi saabView Answer on Stackoverflow
Solution 6 - JquerytaelorView Answer on Stackoverflow