41

how to use substr function in this script I need substr(0,25);

<a class="dep_buttons" href="#"> something text something text something text something text something text something text </a>

$('.dep_buttons').mouseover(function(){
    if($(this).text().length > 30) {
        $(this).stop().animate({height:"150px"},150);
    }
    $(".dep_buttons").mouseout(function(){
        $(this).stop().animate({height:"40px"},150);
    });
});
Ergec
  • 11,608
  • 7
  • 52
  • 62
Val Do
  • 2,695
  • 4
  • 20
  • 35

2 Answers2

76

Extract characters from a string:

var str = "Hello world!";
var res = str.substring(1,4);

The result of res will be:

ell

http://www.w3schools.com/jsref/jsref_substring.asp

$('.dep_buttons').mouseover(function(){
    $(this).text().substring(0,25);
    if($(this).text().length > 30) {
        $(this).stop().animate({height:"150px"},150);
    }
    $(".dep_buttons").mouseout(function(){
        $(this).stop().animate({height:"40px"},150);
    });
});
Moorthy GK
  • 1,283
  • 9
  • 17
8

If you want to extract from a tag then

$('.dep_buttons').text().substr(0,25)

With the mouseover event,

$(this).text($(this).text().substr(0, 25));

The above will extract the text of a tag, then extract again assign it back.

Praveen
  • 55,303
  • 33
  • 133
  • 164