Find & Replace same characters in within different elements

I have several different form labels, each with a unique "for" attribute.  I want to replace a text string that occurs within the values of only those labels with select "for" attirbutes. For example, suppose I want to replace replace  ":" with "*" within the text in all labels where for is lorem or ipsum.

This:
<label for="lorem">LOREM:  </label>
<label for ="ipsum">IPSUM:  </label>
<label for ="dolor">DOLOR:  </label>

Becomes this:
<label for="lorem">LOREM*  </label>
<label for ="ipsum">IPSUM*  </label>
<label for ="dolor">DOLOR: </label>

I'd like to use jquery or plan javascript.

Thanks.
JJ2357Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Rainer JeschorCommented:
Hi,
like this?
$(document).ready(function() {
    $('label[for="lorem"],label[for="ipsum"]').each(function() {
        $(this).text($(this).text().replace(":","*"));
    });
});

Open in new window

Live sample:
http://jsfiddle.net/EE_RainerJ/8wbgxcbw/
HTH
Rainer

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Alexandre SimõesManager / Technology SpecialistCommented:
Here's a sample fiddle: Sample

I'm adding here the code just for reference:
var forArray = ['lorem', 'ipsum'],     // specify the 'for' values here
    replace = { src: ':', dest: '*' }; // specify the replace settings here

for(var i=0; i<forArray.length; i++){
    var elements = $('label[for="' + forArray[i] + '"]');
    for(var j=0; j<elements.length; j++){
        var $elem = $(elements[j]),
            elemHtml = $elem.html();

        $elem.html(elemHtml.replace(replace.src, replace.dest));
    }
}

Open in new window

JJ2357Author Commented:
@Rainer - thanks.  The solution works as requested.  Do you know if there's any way to just replace just the first instance of ":" within the label?  I ask because some of the label values include URL's, and the ":" is getting stripped from those too.
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

Rainer JeschorCommented:
Hi,
thanks for the points.
The replace function by default will only replace the first instance - I have just updated the jsFiddle (same url as above).
As you can see, only the first one gets replaced.
KR
Rainer
Rainer JeschorCommented:
At least in Chrome and IE11 (just tested).
JJ2357Author Commented:
Interesting.  It only  happens when the ":" is included within a tag.  Example: replace the URL in your updated fiddle with:
<img src="https://www.google.com/images/srpr/logo11w.png">

Open in new window

Rainer JeschorCommented:
:-)
Your sample did just contain plain text - so no additional tags. Therefore I used the text() function.
If your label contains html then we have to use the html() function:
$(this).html($(this).html().replace(":","*"));

Open in new window

I updated the fiddle as well.
KR
Rainer
JJ2357Author Commented:
terrific, my bad.  Thanks.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
jQuery

From novice to tech pro — start learning today.