$(document).ready(function() { $("a").click(function() { $("#results").load("jquery-routing.php", { pageNo: $(this).text(), sortBy: $("#sortBy").val()} ); return false; }); });
How do I create an array in jQuery and use that array instead of { pageNo: $(this).text(), sortBy: $("#sortBy").val()}
each function to iterate over elements of arrays, as well as properties of objects. The jquery . each() function can be used to iterate over any collection, whether it is an object or an array. In the case of an array, the callback is passed an array index and a corresponding array value each time.
jQuery array is used to store multiple objects/values in a single array variable. jQuery is a compact and fast JavaScript library. It is rich with lots of new enhanced features. It simplifies HTML document traversal and manipulation, animation, event handling, and Ajax.
You can make an array of int s, double s, or any other type, but all the values in an array must have the same type. To create an array, you have to declare a variable with an array type and then create the array itself. Array types look like other Java types, except they are followed by square brackets ( [] ).
Creating an ArrayUsing an array literal is the easiest way to create a JavaScript Array. Syntax: const array_name = [item1, item2, ...]; It is a common practice to declare arrays with the const keyword.
Some thoughts:
jQuery is a JavaScript library, not a language. So, JavaScript arrays look something like this:
var someNumbers = [1, 2, 3, 4, 5];
{ pageNo: $(this).text(), sortBy: $("#sortBy").val()}
is a map of key to value. If you want an array of the keys or values, you can do something like this:
var keys = []; var values = []; var object = { pageNo: $(this).text(), sortBy: $("#sortBy").val()}; $.each(object, function(key, value) { keys.push(key); values.push(value); });
objects in JavaScript are incredibly flexible. If you want to create an object {foo: 1}
, all of the following work:
var obj = {foo: 1}; var obj = {}; obj['foo'] = 1; var obj = {}; obj.foo = 1;
To wrap up, do you want this?
var data = {}; // either way of changing data will work: data.pageNo = $(this).text(); data['sortBy'] = $("#sortBy").val(); $("#results").load("jquery-routing.php", data);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With