I have a label element:
<form id="new_user">
<div id="first-name">
<label for="user_profile_attributes_first_name"><abbr title="required">*</abbr> First name</label>
</div>
</form>
I want to replace the text First name with Team name when #new_user has .team-registration. How it`s better to do that?
if $('#new_user').hasClass('team-registration') {
....
}
Thanks!
Can’t you just combine both conditions in one selector? As for the actual replacement,
$.htmlaccepts functions too (since jQuery 1.4):Here’s a fiddle!
Note: I’m using
htmlinstead oftextas it appears that#new_usercontains other elements.textwould remove those tags and give you the flat text, whereashtmlacts on the actualinnerHTML.Note:
string.replace(string, string)only replaces the first occurrence. If you need to replace all occurrences, use a regular expression: