0

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!

6
  • why don't you simply use $.trim(songToTrim) Commented Dec 11, 2014 at 4:59
  • @Kartikeya $.trim trims whitespace from the ends, I think stripping all whitespace is the intent. Commented Dec 11, 2014 at 5:00
  • Which error? Also try .text().toLowerCase().replace... Commented Dec 11, 2014 at 5:01
  • Where is this code run. What is the actual error? Commented Dec 11, 2014 at 5:02
  • @AlexanderO'Mara Im trying to get the code up on fiddle or something. Its kinda complicated (for me) with handlebar templates and JSON file so it's taking me a while. Worst case I will put it up on my server Commented Dec 11, 2014 at 5:04

2 Answers 2

1

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

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

Comments

0

Use it like this:

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

2 Comments

I am not receiving an error anymore but the variable still has white space
try using replace(/^\s+|\s+$/g,'')

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.