Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use substr() function in jquery? [closed]

Tags:

jquery

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);     }); }); 
like image 747
Val Do Avatar asked Jan 29 '14 05:01

Val Do


People also ask

Is substr () and substring () are same?

The difference between substring() and substr()The two parameters of substr() are start and length , while for substring() , they are start and end . substr() 's start index will wrap to the end of the string if it is negative, while substring() will clamp it to 0 .

How do you substring in JQ?

To get substring of a string in jQuery, use the substring() method. It has the following two parameters: from: The from parameter specifies the index where to start the substring. to: The to parameter is optional.

How do you grab substring after a specified character jQuery or Javascript?

To get the substring after a specific character:Use the split() method to split the string on the character. Access the array of strings at index 1 . The first element in the array is the substring after the character.

What is the use of substr () in string?

The substring() method extracts characters, between two indices (positions), from a string, and returns the substring. The substring() method extracts characters from start to end (exclusive).


2 Answers

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);     }); }); 
like image 56
Moorthy GK Avatar answered Sep 22 '22 16:09

Moorthy GK


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.

like image 32
Praveen Avatar answered Sep 20 '22 16:09

Praveen