1

I have this code:

<div>Text: <a href="#">Fly London</a></div>

I want to change the text "Text:" with JS, so I used this:

<script>
     $(document).ready(function(){ 
          $('.p-detail-info > div:contains("Text:")').text('Another text:');
     });
</script>

But if I do it, the URL will disappear. How to change it if I want to keep URL?

Thank you!

1
  • I'd add jQuery as a tag to your question. Commented Jun 7, 2018 at 7:50

3 Answers 3

2

You can also do this by simply add span tag and change the span tag text

$(document).ready(function(){ 
$('.p-detail-info > div > span:contains("Text:")').text('Another text:');});
   





 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>

<div class="p-detail-info">
<div><span>Text:</span> <a href="#">Fly London</a></div>
</div>

Sign up to request clarification or add additional context in comments.

Comments

1

You'll have to select the text node, which is not possible with selectors alone, unfortunately. Select the parent div, get its first childNode (which is the text node we want), and then assign to the text node's textContent, no jQuery required:

document.querySelector('div').childNodes[0].textContent = 'Another Text';
<div>Text: <a href="#">Fly London</a></div>

Comments

1

Following should work fine for changing the text:

<script>$(document).ready(function(){$('.p-detail-info > div').text('Another text:');});<script>

Cheers, Happy coding.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.