Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8728161
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T08:34:08+00:00 2026-06-13T08:34:08+00:00

I have little trouble understanding why variables aren’t working properly in the following code.

  • 0

I have little trouble understanding why variables aren’t working properly in the following code.

var welcomeMsg = $('#accountbar span.welcome_text').text(); // This returns "Welcome, Nick(account)      "
var cutdown = welcomeMsg.slice(0, -6); // This removes excessive spaces after (account) 
cutdown = cutdown.slice(cutdown.indexOf(",")+2,cutdown.indexOf("(")); // returns "Nick"

var username = cutdown;  // "Nick" //This doesn't work with last line of the script
//alert(username);
//var username = "Nick"  // but this works if it's used instead of  var username = cutdown;
//alert(username);

$("td:contains('"+username+"')").parent().css({
    "background" : "url('http://imagehost.com/forum/row1.png')"
});

It’s for a Greasemonkey script, if that matters.

Solved:
It seems that .trim is better way to delete excessive spaces around the wanted string.
The script works perfectly for me if this

var cutdown = welcomeMsg.slice(0, -6); // This removes excessive spaces after (account) 
cutdown = cutdown.slice(cutdown.indexOf(",")+2,cutdown.indexOf("("));

is replaced with this

var cutdown = welcomeMsg.slice(welcomeMsg.indexOf(",")+2,welcomeMsg.indexOf("("));
cutdown = cutdown.trim();
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T08:34:09+00:00Added an answer on June 13, 2026 at 8:34 am

    .trim fixed the problem when it is used instead of .slice

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Little trouble in using css, in the below code, i have used odd and
Im working on a CS assignment and Im having a little trouble understanding how
I am having a little trouble understanding migrations in Ruby on Rails. I have
I have a little trouble understanding the security bit around JSON, because often things
I have trouble understanding lambda functions and the mechanism to capture variables, so I
I have a little trouble understanding scope and lifetime using javaScript and jQuery. I
I have a little trouble in understanding a wrapper class. It would be great
I a having a little trouble with vector or array operations. I have three
I'm having a trouble with this little problem. Let's say, I have an array,
I'm a jQuery newbie, and I have trouble with a little script I created.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.