Παρασκευή 17 Φεβρουαρίου 2012

Jquery split & join example

Here I will show you how you can use jquery in order to wrap every 2nd word of a paragraph in a span.

So, if you have for example the following paragraph into your html:


and your stylesheet is:


by putting this:




your final result will be:

This is a test paragraph.

First you use split method, to split your sentence into single words. Then you store into a variable called numWords the size of the array. In a for loop, you check if your number is even and if that's the case, you wrap the specific element in a span. After this iteration, you use join, in order to show whole sentence.


Δεν υπάρχουν σχόλια:

Δημοσίευση σχολίου