JQuery text () with replacement () does not replace text
I have the following H2:
<h2 id="resetPWSuccess">Password reset instructions have been sent to *|RESETPASSWORDEMAIL|*</h2> I want to replace *|RESETPASSWORDEMAIL|* with the current email address that I have in the jQuery emailAddress variable.
I tried the following, but it does not work:
$('h2#resetPWSuccess').text().replace('*|RESETPASSWORDEMAIL|*', emailAddress).show(); Is there a way to update h2 text - I prefer a single line if possible.
Use this instead:
$('#resetPWSuccess').text( $('#resetPWSuccess').text().replace('*|RESETPASSWORDEMAIL|*', emailAddress) ).show(); Note that I also removed h2 from your selector: this is useless and slower than just using the id. When you query jQuery to search for #resetPWSuccess , it uses the very fast native getElementById function.
From jQuery source code:
// Shortcuts if ( (match = rquickExpr.exec( selector )) ) { // Speed-up: Sizzle("#ID") if ( (m = match[1]) ) { if ( nodeType === 9 ) { elem = context.getElementById( m );