user2980869
user2980869

Reputation: 59

Jquery Trim all spaces from variable

On a click I store a variable. I would like to then trim that variable for all whitespace. I am getting an error when I to simply console.log the new variable.

var songToTrim = $(this).html();
console.log(songToTrim);
var songToPlay = songToTrim.replace(/ /g,'');
console.log(songToPlay);

For bonus points if you can add how to make sure the variable is converted to all lowercase that would be huge. If not it'll still work. Thanks!

Upvotes: 0

Views: 2752

Answers (2)

VSri58
VSri58

Reputation: 3761

Here is my solution

Code:

 $("#test").on('click', function(){
    var songToTrim = $(this).html();
   var trm  = songToTrim.replace(/ /g,'');
   var songToPlay = trm.toLowerCase();
    });

Use .toLowerCase() to convert your variable to Lower Case.

The error you are getting is because you have a typo in your code somewhere. You have misspelled Console as Consle.

Find it and correct it.

Here is a working Demo

Upvotes: 1

Pragya Sharma
Pragya Sharma

Reputation: 94

Use it like this:

var str = songToTrim.trim().replace(/\s+/g, ' ');
var lower = str.toLowerCase();

Upvotes: 0

Related Questions