Javascript Split Function and JQuery Not Working Together

Get URL variables should be parsed. I created a jQuery document.location object and then 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 an error stating that the object does not have a method each.

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

The code:

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

I also tried using the search property in the first function, but it does not allow this ie $ (document.location.search) gives an error.

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

PS: all the above code is included in the document.ready function from jQuery

+5
source
4

jQuery document.location , DOM.

search $.each intead of .each, , :

$.each(document.location.search.split('&'), function(){
  ...
});
+7

:

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

jQuery .each() jQuery, .

, $(document.location).attr('search').split('&'), JavaScript, , , "": .

jQuery, $.each(), .

+5

, JS- () -.

Try

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

.

0

The jQuery function is each()intended to be called on its own, directly from jQuery; javascript arrays do not have it as a function because it was not built that way.

I think you are looking for this:

$.each($(document.location).attr('search').split('&'), function (index, value) {
    // stuff
});
-1
source

All Articles