最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

substring javascript not working at all - Stack Overflow

programmeradmin2浏览0评论

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"

 }

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"

 }
Share Improve this question edited Nov 17, 2012 at 20:41 user1066524 asked Nov 17, 2012 at 20:19 user1066524user1066524 3732 gold badges11 silver badges24 bronze badges 6
  • 6 This is not the code causing your error. It is valid. Look to the code before it for your invalid assignment. – Michael Berkowski 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. – user1066524 Commented Nov 17, 2012 at 20:30
  • You're missing an } at the end. Not sure if that's known or not. – Jamund Ferguson 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. – user1066524 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. – user1066524 Commented Nov 17, 2012 at 20:48
 |  Show 1 more ment

3 Answers 3

Reset to default 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:

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.

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.

发布评论

评论列表(0)

  1. 暂无评论