1

I have the following div and want to change the text that says "Don't have an account?" to something else.

I've tried $('a#createAccount').text'some text) or .html('some text'); Same with $('.create p'), but it removes the <a>.

<div class="create">
  <p>
    Don't have an account?<a id="createAccount" tabindex="1" href="/b3c3aa0d-d4db-459d-8bf6-ed1538d45256/B2C_1_sign_up_sign_in_persona...">Sign up now</a>
  </p>
</div>

change only the text leaving the <a> unchanged

Ivar
  • 5,377
  • 12
  • 50
  • 56
Amy Ruddy
  • 53
  • 6
  • 2
    Maybe try adding the text in tags and manipulate that tag instead the whole link tag. also you mispelled yout Jquery it should be $('a#createAccount').text('some text'); here is a snippet: https://codepen.io/manuelpirez/pen/ExYEGvY – Manuel Pirez Sep 10 '19 at 17:41

5 Answers5

2

You can select the anchor tag, put it in a variable, overwrite the content of your paragraph and then append the anchor tag back to it.

let anchor = $('a#createAccount');
let paragraph = $('.create p')
paragraph.text("New text ");
paragraph.append(anchor);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="create">
  <p>
    Don't have an account?<a id="createAccount" tabindex="1" href="/b3c3aa0d-d4db-459d-8bf6-ed1538d45256/B2C_1_sign_up_sign_in_persona...">Sign up now</a>
  </p>
</div>
Ivar
  • 5,377
  • 12
  • 50
  • 56
1

Per How do I select text nodes with jQuery?, you can leverage chained functions to filter your matched elements to the text nodes within a parent element, then set their data attribute to the text that you want:

$(document).ready(function() {
  $(".create p").contents().filter(function() {
    return this.nodeType === 3; //Node.TEXT_NODE
  }).each(function() {
    if ($(this).text().trim() !== "") this.data = 'some text'; // ensure that stray blank text nodes aren't caught up in this change
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="create">
  <p>
    Don't have an account?<a id="createAccount" tabindex="1" href="/b3c3aa0d-d4db-459d-8bf6-ed1538d45256/B2C_1_sign_up_sign_in_persona...">Sign up now</a>
  </p>
</div>
esqew
  • 39,199
  • 27
  • 87
  • 126
0

var link = $("#createAccount");
$(".create p").html("some text&nbsp;").append(link);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="create">
  <p>
    Don't have an account?<a id="createAccount" tabindex="1" href="/b3c3aa0d-d4db-459d-8bf6-ed1538d45256/B2C_1_sign_up_sign_in_persona...">Sign up now</a>
  </p>
</div>
Below the Radar
  • 6,947
  • 10
  • 58
  • 131
-2

Try using document.write and update what you need

    document.write("<p>
    Don't have an account?<a id="createAccount" tabindex="1" href="/b3c3aa0d-d4db-459d-8bf6-ed1538d45256/B2C_1_sign_up_sign_in_persona...">Sign up now</a>
  </p>");
-2

Do you have to use jQuery to do this? If not and you can just use plain old javascript, why not try document.getElementById('createAccount').innerHTML = 'some text';

So after understanding what you were asking, I was able to accomplish this with the below:

<p id="MP"> Some text to replace <a href="#">My link</a> </p> 
<br><br> <input type="button" value="Change" onclick="Change()"/> 

<script type="text/javascript">

    function Change() { var Markup = document.getElementById('MP').innerHTML; var POS = Markup.indexOf('<a'); Markup = Markup.substring(POS); Markup = 'Some new text ' + Markup; document.getElementById('MP').innerHTML = Markup; }

</script>
sys_adm_dev
  • 63
  • 1
  • 7
  • Because that changes the text on the link. I am targeting the text before the link. – Amy Ruddy Sep 10 '19 at 17:43
  • Oh I see what you're saying now, I misunderstood. My bad. Let me go back to the drawing board on this and see if I can't do what you want. – sys_adm_dev Sep 10 '19 at 17:45