Password reset instructions have been s...">

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.

+4
source share
5 answers

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 ); 
+10
source

I suggest a different approach:

 <h2 id="resetPWSuccess">Password reset instructions have been sent to <span id="resetPWemail"></span></h2> 

And the corresponding js:

 $('#resetPWemail').text(emailAddress); $('#resetPWSuccess').show(); 
+4
source

Use this.

 $('#resetPWSuccess').text( $('#resetPWSuccess').text().replace('*|RESETPASSWORDEMAIL|*', ' neeraj@gmail.com ') ) 

Watch Demo Demo

+2
source

try it

 $("#resetPWSuccess").text( $('#resetPWSuccess').text(). replace('*|RESETPASSWORDEMAIL|*', $("#EmailID").val())) 
+2
source

You can even just do it.

  <h2 id='resetPWSuccess'></h2> $('h2#resetPWSuccess').text('Password reset instructions have been sent to '+emailaddress).show(); 
+2
source

Source: https://habr.com/ru/post/1485572/


All Articles