---
title: "trim() method in jQuery"  
description: "trim() method in jQuery"  
author: "Ethan Karla"  
published: 2021-07-28  
updated: 2023-06-14  
canonical: https://www.mindstick.com/forum/156655/trim-method-in-jquery  
category: "jquery"  
tags: ["jquery"]  
reading_time: 2 minutes  

---

# trim() method in jQuery

What is the trim() [method in jQuery](https://www.mindstick.com/forum/214/size-method-in-jquery)? [Explain](https://www.mindstick.com/forum/157854/what-is-system-debugging-explain-some-system-debugging-tools-used-in-modern-computer-systems) it with the help of an example?

## Replies

### Reply by Aryan Kumar

The jQuery trim() [method](https://www.mindstick.com/forum/166/webservice-method) is used to remove whitespace from the beginning and end of a string. It is a chainable method, which means that you can call other jQuery methods on the returned object.

The trim() method takes no arguments.

Here is an example of how to use the trim() method:

Code snippet

```plaintext
// Select the element with the id 'my-element'
var element = $('#my-element');

// Get the text content of the element
var text = element.text();

// Trim the whitespace from the beginning and end of the text
var trimmedText = text.trim();
```

The trim() method can also be used to remove whitespace from the beginning and end of an attribute value. For example, the following code will remove the whitespace from the beginning and end of the href attribute value of the element with the id 'my-element':

Code snippet

```plaintext
// Select the element with the id 'my-element'
var element = $('#my-element');

// Get the href attribute value of the element
var href = element.attr('href');

// Trim the whitespace from the beginning and end of the href attribute value
var trimmedHref = href.trim();
```

The trim() method is a useful tool for removing whitespace from strings and attribute values. It can be used to improve the readability of your code and to make sure that your code is consistent.

Here are some additional examples of how the trim() method can be used:

// Trim the whitespace from the beginning and end of the value of the input element with the id 'my-input' $('#my-input').val().trim();

// Trim the whitespace from the beginning and end of the text content of all elements with the class 'my-class' $('.my-class').text().trim();


---

Original Source: https://www.mindstick.com/forum/156655/trim-method-in-jquery

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
