views:

166

answers:

7
$(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()}

A: 

I haven't been using jquery for a while but you might be looking for this:

jQuery.makeArray(obj)

Shimmy
+2  A: 

Not completely clear what you mean. Perhaps:

<script type="text/javascript"> 
$(document).ready(function() {
  $("a").click(function() {
    var params = {};
    params['pageNo'] = $(this).text();
    params['sortBy'] = $("#sortBy").val();
    $("#results").load( "jquery-routing.php", params );
    return false;
  });
}); 
</script>
Matthew Flaschen
your code is equivalent to what he had. a hash is a hash
mkoryak
Yes, but he's also coming from php, where arrays are also hashes... In JavaScript, its not a 'hash' either - its just an object. +1 - this was my interpretation of the question as well..
gnarf
I know the difference between JavaScript arrays and objects. However, it seemed like the real point of the question was to construct the parameters dynamically.
Matthew Flaschen
A: 

Here is an example that I used.

<script>
  $(document).ready(function( )
  {

    var array =    
      $.makeArray(document
      .getElementsByTagName(“p”));
    array.reverse( ); 
    $(array). appendTo(document.body);
  });
</script>
CodeToGlory
A: 

your question makes no sense. you are asking how to turn a hash into an array. You cant.

you can make a list of values, or make a list of keys, and neither of these have anything to do with jquery, this is pure javascript

mkoryak
... in other words, an Array is an Array, a Hash is a Hash. Use whatever is appropriate for the situation, they're not interchangeable.
deceze
... Actually, they are both objects ...
gnarf
A: 

Some thoughts:

  • jQuery is a JavaScript plugin, 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);
ojrac
vick
ojrac
A: 
<script type="text/javascript"> 
$(document).ready(function() {
$("a").click(function() {
var params = {};
params['pageNo'] = $(this).text();
params['sortBy'] = $("#sortBy").val();
$("#results").load( "jquery-routing.php", params );
        return false;
    });
}); 
</script>

im using that so far.. this is the problem I am having ...

I am able to set the PageNo with:

<a href="jquery-routing.php?p=1">1</a>
<a href="jquery-routing.php?p=2">2</a>

but how do I pass on the SortBy to my jquery-routing.php ??

Basically, if this statement is true, I want to set the SortyBy to SortBy ='errors';

if( isset($_POST['display']) && $_POST['display'] == 'errors' ) {.. }

Maybe this is not the correct way to do it? I have a page that lists data rows from a database, I want to have a paginator and I also want to be able to sort the data on criteria. So if the user clicks on the "name" header in the table I it to sort the list by name and so on..

vick
A: 

You may be confusing Javascript arrays with PHP arrays. In PHP, arrays are very flexible. They can either be numerically indexed or associative, or even mixed.

array('Item 1', 'Item 2', 'Items 3')  // numerically indexed array
array('first' => 'Item 1', 'second' => 'Item 2')  // associative array
array('first' => 'Item 1', 'Item 2', 'third' => 'Item 3')

Other languages consider these two to be different things, Javascript being among them. An array in Javascript is always numerically indexed:

['Item 1', 'Item 2', 'Item 3']  // array (numerically indexed)

An "associative array", also called Hash or Map, technically an Object in Javascript*, works like this:

{ first : 'Item 1', second : 'Item 2' }  // object (a.k.a. "associative array")

They're not interchangeable. If you need "array keys", you need to use an object. If you don't, you make an array.


* Technically everything is an Object in Javascript, please put that aside for this argument. ;)

deceze