5

The Get variables of the URL need to be parsed. I have made a jQuery object of document.location and then have used the attr function to get the search attribute to get all the variables. But when i use the split function on it and after that each() is used it gives error stating that the object does not have a method each .

TypeError: Object [object Array] has no method 'each'  

Code is :

 $(document.location).attr('search').split('&').each()

I have also tried to use the search property in the first function but it does not allow it i.e $(document.location.search) gives error.

I have also checked that data type of the what is returned by split function, the console says that it is an object, i am also confused that it should have been an array.

P.S : all the above code goes in document.ready function of jQuery

S. A. Malik
  • 3,465
  • 6
  • 37
  • 56
  • possible duplicate of [How can I get query string values?](http://stackoverflow.com/questions/901115/how-can-i-get-query-string-values) – Chris Farmiloe Apr 05 '13 at 12:30

4 Answers4

7

Making a jQuery object from the document.location object is pointless, because it's not a DOM element.

Just get the search property from the object, and use the $.each method intead of .each as you are looping an array, not elements:

$.each(document.location.search.split('&'), function(){
  ...
});
Guffa
  • 687,336
  • 108
  • 737
  • 1,005
  • 1
    downvoter - this is almost the right answer - Guffa - you can't call `.each()` on the result of `split` ! – Alnitak Apr 05 '13 at 12:31
  • 1
    document.location.search.split('&').each() TypeError: Object [object Array] has no method each – S. A. Malik Apr 05 '13 at 12:34
  • @backTangent: I already fixed that part too. Use `$.each` instead of `.each`. – Guffa Apr 05 '13 at 12:36
  • @Guffa: the problem I am facing now is that i can not get the array values when i loop through them, $(this) has a typeof Object but if i use text() method it says no such method, if i use match() it says no such method – S. A. Malik Apr 05 '13 at 18:21
  • 1
    @backTangent: You are looping though an array of strings, so using `$(this)` is just as pointless as `$(document.location)` was. You can't get the text that the element contains, because it's not an element at all. You can't use the `match` method because you have wrapped the string in a jQuery object. Just use `this.match()`. – Guffa Apr 05 '13 at 18:36
5

Try this:

$.each($(document.location).attr('search').split('&'), function (index, value) {
    alert(index + ': ' + value);
});

jQuery .each() method is used to iterate over a jQuery object, executing a function for each matched element.

But what you get from the $(document.location).attr('search').split('&') is a JavaScript array, which obviously has no method 'each': that is why you are getting the error.

To loop through an array in jQuery you need to use $.each() method like above.

рüффп
  • 5,172
  • 34
  • 67
  • 113
palaѕн
  • 72,112
  • 17
  • 116
  • 136
  • I have tried this way and it works but I am more interested in what is causing the issue. Will help build by knowledge. – S. A. Malik Apr 05 '13 at 12:31
  • @backTangent the real issue is trying to use `.attr()` on something that isn't a DOM element wrapped in a jQuery object. Guffa's answer is mostly correct, and this one is wrong. – Alnitak Apr 05 '13 at 12:32
  • 1
    @backTangent: It "works" because you can create a jQuery object from an object that isn't an element, and it currently works to use `attr` method to get an attribute from that object although it's not documented. It's not how jQuery is supposed to be used, and as it is undocumented there is no guarantee that it will continue to work. – Guffa Apr 05 '13 at 12:44
  • The `attr` method is not documented to be used with a plain object wrapped in a jQuery object. See http://api.jquery.com/jQuery/#working-with-plain-objects – Guffa Apr 05 '13 at 12:54
0

As far as I know, JS-arrays don't have a each()-function.

Try

var search_arr = $(document.location).attr('search').split('&');
for (var s in search_arr) {
    console.log(search_arr[s];
}

instead.

Vince
  • 1,517
  • 2
  • 18
  • 43
  • please try reading other answers and comments before answering. Whilst you're strictly correctly, the real problem is that you can't call `.attr` on `document.location`. – Alnitak Apr 05 '13 at 12:40
  • As I was typing my answer, there were no other answers. Sorry about that. – Vince Apr 05 '13 at 12:50
-1

jQuery's each() function is meant to be called on its own, directly from jQuery; javascript arrays don't have it as a function, because it wasn't built that way.

I think you're looking for this:

$.each($(document.location).attr('search').split('&'), function (index, value) {
    // stuff
});
DiMono
  • 3,308
  • 2
  • 19
  • 40