Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use String.replace() to replace ALL occurrences of a pattern

In many divs I have to change the class name by replacing spaces with periods....

So I tried this

$(jqueryElements).each(function(index)
{ 
    jqueryElements[index].className.replace(' ','.');
});

When the class has two words it works fine... But when the class name has 3 or more words it fails....

className='one word';
jqueryElement[index].className.replace(' ','.'); // console-> one.word
className='many many words';
jqueryElement[index].className.replace(' ','.'); // console-> many.many words

There is something wrong??

I'm using Chrome 25, Win7, jQuery 1.8

EDIT 2

I need to replace spaces to search all the span elements that have a particular class name.

So I use jquery in this way...

$('#span-container').find('span.'+jqueryElements[index].className.replace(' ','.').text('there are '+span_counter+'spans with this classname');

The result of this request shoud be:

 $('#span-container').find('span.many.many.words).text('there are '+span_counter+'spans with this classname');

Instead I have:

$('#span-container').find('span.many.many words).text('there are '+span_counter+'spans with this classname');
like image 208
andrescabana86 Avatar asked Jan 29 '26 19:01

andrescabana86


1 Answers

Don't use replace, use split and join:

jqueryElements[index].className.split(' ').join('.');
like image 139
Barmar Avatar answered Feb 01 '26 12:02

Barmar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!