2

Can someone tell me if their is a way to limit text within div classes.

example

<div class="mon">December</div>
<div class="mon">November</div>
<div class="mon">October</div>

$('.mon').hide().text();

var month = $('.mon').text().substr(0,3);

$('.mon').append(month).show();
alert(month);

trying to output

Dec Nov Oct

Any help?

0

2 Answers 2

3

.text() can take a function, like this:

$('.mon').text(function(i, t) { return t.substr(0, 3); });

You can test it out here.

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

Comments

2

You can do this:

$('div.mon').text(function() {
    return $(this).text().substring(0, 3);
});

Live example

text, like a lot of other "setter" functions of jQuery (html, attr, etc.), accepts a function it will call to get the value to set for each element.

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.