0

I have this code, that takes a string and splits it into an array:

nodes = $("#" + model_id + "-" + node_class + "--" + "title").data("nodes").split(",")

When there is only one element in the string (no commas), the variable "nodes" does not become an array, but a regular variable. So when I try to iterate over each element in "nodes", nothing happens if the original string only contains one element. If it has several elements, everything is OK.

$.each(nodes, function (id, node_id) {
  if ($("#" + model_id + "-" + node_class + "-" + node_id + "-" + "chkbx").is(":checked")) {
    counter ++
  }
})

I have tried to declare "nodes" as an array, but when I assign the splitted string, it's all the same. Since I use a "split" to assign values I don't think I can use "push" to append values to the array.

I have tried to put square brackets everywhere, I think, i.e. like this:

[nodes] = $("#" + model_id + "-" + node_class + "--" + "title").data("nodes").split(",")

... but that didn't help.

Is there any solutions to this, except from checking if "nodes" is an array or not, and then write different code to handle both options?

2
  • 1
    Could you post some kind of demo that reproduces your problem (or at least explains/demonstrates it) at JS Fiddle or similar? There might be an alternative approach to the problem that I can't see from just looking at your jQuery. Commented Jan 2, 2012 at 14:19
  • Split should always return an array for just this reason. I second the comment from @DavidThomas that a complete example would be helpful. Commented Jan 2, 2012 at 14:38

1 Answer 1

5

When there is only one element in the string (no commas), the variable "nodes" does not become an array, but a regular variable.

A) Variables referring to arrays are regular variables. B) That's not how split works. split always returns an array. If the delimiter isn't present in the string, the resulting array is one element in length. (Live proof) So as long as data returns a string, nodes will be set to an array. Note, though, that data does not always return a string, and so data("nodes").split(",") may fail with the error that split is not a function, because data can return null or an object as well as string. If you know it will always be a string because of your application logic, that's fine, but if so nodes will always be an array, which is why I mention it.

Re your comment below: Iteration works just fine on single-element arrays: http://jsbin.com/ehucop/2

I suspect you need to look at the JavaScript console in your browser, I bet you'll find an error occurring which is preventing your iteration code from running at all.

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

5 Comments

OK. Thank's for explaining this. My problem is still the same. If the string has one element, the iteration doesn't seem to work.
@JohanHovda: The problem is in code you haven't quoted, iteration works just fine on single-element arrays: jsbin.com/ehucop/2
@JohanHovda I suppose it could depend on some wrong name for the check boxes you are looping on...
@T.J. Crowder: Thank's a lot for the extended explanation. To turn the data into a string I replaced '.data("nodes")' with '.attr("data-nodes")', and now it works.
@JohanHovda: Glad you sorted it out, I expect that's because attr always returns a string (well, except for tabindex and such). I guess my paranoia about using data to access data-* attributes is justified (I always use attr to get them, because data is too overloaded with other functionality). Although in a simple test, it seems like data returns strings as well: jsbin.com/uyoyed Your page must be more complex. :-)

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.