4

I have the following H2:

<h2 id="resetPWSuccess">Password reset instructions have been sent to *|RESETPASSWORDEMAIL|*</h2>

I want to replace the *|RESETPASSWORDEMAIL|* with current email address that I have in the jQuery variable emailAddress.

I have tried the following but it doesn't work:

$('h2#resetPWSuccess').text().replace('*|RESETPASSWORDEMAIL|*', emailAddress).show();

Is there a way to update the h2 text - I prefer one line if possible.

5 Answers 5

10

Use this instead :

$('#resetPWSuccess').text(
   $('#resetPWSuccess').text().replace('*|RESETPASSWORDEMAIL|*', emailAddress)
).show();

Note that I also removed the h2 from your selector : it's useless and slower than just using the id. When you ask jQuery to search for #resetPWSuccess, it uses the very fast native getElementById function.

From jQuery's source code :

// Shortcuts
if ( (match = rquickExpr.exec( selector )) ) {
    // Speed-up: Sizzle("#ID")
    if ( (m = match[1]) ) {
        if ( nodeType === 9 ) {
            elem = context.getElementById( m );
Sign up to request clarification or add additional context in comments.

3 Comments

worked great and will accept - just time limit pending. Can I ask about dropping the h2 (for my education). How come this is faster? Does it alter the jquery search of the DOM somehow?
appreciate more then I can say - never would have know that... many updates now to do :)... thankyou
You have the same property with class based selector. If '.theclass' matches your need, don't add anything to the selector.
4

I'd 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();

Comments

2

Use like that.

$('#resetPWSuccess').text(
  $('#resetPWSuccess').text().replace('*|RESETPASSWORDEMAIL|*', '[email protected]')
)

See Demo Demo

Comments

2

Try this

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

Comments

2

You can even simply do this

 <h2 id='resetPWSuccess'></h2>

 $('h2#resetPWSuccess').text('Password reset instructions have been sent to '+emailaddress).show();

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.