3

is there any reason why this wouldn't be working?

     var caseString = "sliderInput";
     var theString = caseString.substring(1, 2);

I put it through the Firebug debugger in Firefox and it is giving me the error: "invalid assignment left-hand side."

** here is my exact code

        var elements = new Array();
        elements = document.getElementsByTagName("Input");


var allSliderInputs = new Array();
var sliderParams = new Array();
var first, last, inc, style;

for (var i=0; i < elements.length ; i++){
    var c = elements[i].className; //works fine here
    var t = c.substring(0, 2); //when it hits this line it says "invalid assignment left-hand side"

 }
6
  • 6
    This is not the code causing your error. It is valid. Look to the code before it for your invalid assignment. Commented Nov 17, 2012 at 20:24
  • okay, I now see why it is an l and not sl. but the point is it isn't working at all. Commented Nov 17, 2012 at 20:30
  • You're missing an } at the end. Not sure if that's known or not. Commented Nov 17, 2012 at 20:44
  • no, I just didn't add that in the example. I didn't forget a bracket in my actual code. Commented Nov 17, 2012 at 20:45
  • okay well this is odd. According to Firebug this warning means I should use == instead of a = which makes no sense in this situation. Commented Nov 17, 2012 at 20:48

3 Answers 3

4

substring is 0-indexed so you should instead do something like this:

var word = "slider";
var part = word.substring(0,2); // sl

Also take note that .slice() does the same thing but is actually more powerful, because it can count backwards as well as forwards.

To solve your new problem I would suggest a few things:

  1. You need to cache your length value. The list returned by getElementsByTagName is live meaning any changes to your list while you're looping will effect that value, so it won't behave as you'd expect.
  2. Don't use new Array() it's overly fancy.
  3. You don't need to instantiate variables that you're going to define right after.

Try this:

var elements = document.getElementsByTagName("input");
var allSliderInputs = [];
var sliderParams = [];
var len = elements.length;
var first, last, inc, style, c, t, i;

for (i = 0; i < len; i++) {
    c = elements[i].className; //works fine here
    t = c.substring(0, 2); 
    console.log(t);
}

This works fine for me in Firefox when run on this very page in stackoverflow: sample result

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

4 Comments

yeah it still isn't working. I wrote this question as an example. But here is my exact code. var elements = new Array(); elements = document.getElementsByTagName("Input"); var allSliderInputs = new Array(); var sliderParams = new Array(); var first, last, inc, style; for (var i=0; i < elements.length ; i++){ var c = elements[i].className; //works fine here var t = c.substring(0, 2); //as soon as I hit this line it tells me there is an invalid assignment left-hand side "
So the obligatory, jquery could do this in 1 line comment is probably warranted here. $('input').each(function() { var t = this.className.subString(0, 2); })
thanks for your help. It turns out firebug wasn't really giving me an accurate error on the console. it was telling me line 79 with the substring.....when it reality all it really was..was the missing == in my if statement on line 80.
Yes, be weary of line numbers ;-) Glad you were able to get it!
3

First, .substring (and .substr) is 0-based, not 1-based.

.substring extracts a string between two positions. E.g. .substring(1,4) returns the 2nd, 3rd, and 4th characters. It will stop at position 4.

.substr extracts a string based on start + length. .substr(1,4) returns the first 4 characters starting with the 2nd character.

1 Comment

okay, I now see I made a mistake on the fact that it is of course zero based and not one based, but why is the debugger haulting this in the first place? it happens right when it approaches the substring method.
2

var theString = caseString.substring(1, 2); should return you just l. The substring method is accepting as first argument the start of the substring and as second argument the end of the substring. You're giving it index 1 which is the second character (0 is the first).

You probably have mistaken substring with substr. substr accepts as first argument start index and length as second. So:

var caseString = "sliderInput";
var theString = caseString.substr(0, 2); //sl

is giving the result you want.

Comments

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.