Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to change text value within an element?

Tags:

jquery

replace

How do you change the text for all within the a to

continue reading

using jquery

<div class="post-read">
<a href="http://www.google.com">Read More</a>
</div>
like image 334
Yusaf Khaliq Avatar asked Nov 01 '11 00:11

Yusaf Khaliq


People also ask

Can I put text inside a div?

Using CSS, you can center text in a div in multiple ways. The most common way is to use the text-align property to center text horizontally. Another way is to use the line-height and vertical-align properties. The last way exclusively applies to flex items and requires the justify-content and align-items properties.

What is innerText and innerHtml?

innerText returns all text contained by an element and all its child elements. innerHtml returns all text, including html tags, that is contained by an element.

Which of the following property actually holds the text content of div element?

The textContent property returns: The text content of the element and all descendaces, with spacing and CSS hidden text, but without tags.


3 Answers

Do it with jQuery inside of a document ready handler ($(fn))...

$('.post-read a').text('continue reading');

jsFiddle.

For the sake of it, here is how to do it without jQuery....

var anchor = document.getElementsByClassName('post-read')[0].getElementsByTagName('a')[0],
    textProperty;

if (anchor.textContent) {
    textProperty = 'textContent';
} else if (anchor.innerText) {
    textProperty = 'innerText';
}
anchor[textProperty] = 'continue reading';

jsFiddle.

This will work good for your piece of HTML, but it isn't too generic.

If you don't care about setting innerText property, you could use...

anchor.textContent = anchor.innerText = 'continue reading';

I wouldn't recommend it though.

like image 180
alex Avatar answered Oct 20 '22 13:10

alex


This should do it:

$('.post-read a').html('continue reading');
like image 45
Clive Avatar answered Oct 20 '22 11:10

Clive


$('.post-read a').html("continue reading")
like image 22
Dogbert Avatar answered Oct 20 '22 12:10

Dogbert