Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript Split Function and Jquery not working together

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

like image 684
S. A. Malik Avatar asked Apr 05 '13 12:04

S. A. Malik


People also ask

Why split is not working in jQuery?

Conclusion # The "split is not a function" error occurs when we call the split() method on a value that is not of type string. To solve the error, convert the value to a string by using the toString() method before you call split() , or make sure to only call the split method on strings.

Can you combine jQuery and JavaScript?

It is possible to run jQuery and JavaScript together in you application. All you need to know is, which is a JavaScript Object when trying to run some methods of it. jQuery can be handy sometimes.

What does split do in jQuery?

The split() method splits a string into an array of substrings. The split() method returns the new array. The split() method does not change the original string.

What is the opposite of Split in JavaScript?

The split() method splits a String object into an array of string by separating the string into sub strings. The reverse() method reverses an array in place. The first array element becomes the last and the last becomes the first. The join() method joins all elements of an array into a string.


2 Answers

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(){
  ...
});
like image 69
Guffa Avatar answered Nov 09 '22 23:11

Guffa


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.

like image 24
palaѕн Avatar answered Nov 09 '22 22:11

palaѕн