Link to home
Start Free TrialLog in
Avatar of greddin
greddinFlag for United States of America

asked on

How do a replace text within a specific span class where multiple span classes of the same name exist?

For example, if my page has several spans like this:

<span class="ms-formdescription">Enter the website address:</span>
<span class="ms-formdescription">Type the description:</span>

How do I replace only the span with "Type the description:" with "Enter the name of the site:"?

I've tried this but it doesn't work:

$('span.ms-formdescription').filter(function () { return $(this).text() === 'Type the description:' }).text('Enter the name of the site:');

Thanks for your help!
ASKER CERTIFIED SOLUTION
Avatar of zephyr_hex (Megan)
zephyr_hex (Megan)
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of greddin

ASKER

Thank you.  It's working but I'm having undesirable results.  It's replacing everything in a section because all the span's are nested within a parent span with no id or class.

I should have showed this as my code before.  What I have actually looks like this:

<span dir="none">
<span class="ms-formdescription">Enter the website address:</span>
<span class="ms-formdescription">Type the description:</span>
</span>

Right now the selector is replacing everything within the upper span tag.  Is there a way to match just the second span containing "Type the description:"?

Thanks again.
Avatar of greddin

ASKER

Ok, I have it now.  This works:

$("span.ms-formdescription:contains('Type the description:')").text("This is the new text");

Thanks for your help.
Avatar of greddin

ASKER

I'm good now. Thanks.