1

I am trying to print a value from an array in jQuery to the screen using the .html function. The variable is test.

If I just use the line of code below it will print everything in the array:

$(this).html(test).addClass('messageboxerror').fadeTo(900,1);

Array ( [0] => 2820 Prairie [1] => 19316 [2] => 2820 Prairie [3] => Beloit [4] => 53511 [5] => [6] => 2012-01-17 [7] => [8] => union [9] => Rock County )

I have tried putting .html(test[0]) but this broke the script.

Thanks,

xXPhenom22Xx
  • 1,265
  • 5
  • 29
  • 63
  • hmmm.. try .html( "" + test[0] ) ? – EdH Feb 03 '12 at 20:34
  • That's not how you build an array in JS – Diodeus - James MacFarlane Feb 03 '12 at 20:34
  • Need more content, where is test defined? Also what is `Array ( [0] => 2820 Prairie [1] => 19316 [2] => 2820....` supposed to be? – Chad Feb 03 '12 at 20:34
  • This is an AJAX script that goes to a PHP script that queries the database and returns an array back to AJAX called test. I just need a way to scho out the individual values in the script. The variable is defined here: `$.post("lib/ajax_load_job_detail.php",{ primaryid:$(this).val() } ,function(data) { var test = data;` – xXPhenom22Xx Feb 03 '12 at 20:41
  • And this array is made up of an address and some other details about a given job. – xXPhenom22Xx Feb 03 '12 at 20:44
  • If I do .html(test) it will echo out on the screen the array text you see above. But yes the array was built in PHP then sent back to the AJAX script – xXPhenom22Xx Feb 03 '12 at 20:45

2 Answers2

4

Edit: Check this post on how to send JSON data.

Arrays in javascript is constructed as below,

var test = ["2820 Prairie", "19316", "2820 Prairie", "Beloit", "53511", "", "2012-01-17", "", "union", "Rock County"];

Now test[0] will return you "2820 Prairie".

MDN Array Object Reference

Community
  • 1
  • 1
Selvakumar Arumugam
  • 79,297
  • 15
  • 120
  • 134
  • I think that is my problem. The array was built in PHP and sent over to an AJAX script with the format of Array ([0] => '2820 Prairie',..... When I try .html(test[0]) I get the following response: "A" – xXPhenom22Xx Feb 03 '12 at 20:51
  • This is from the php script: ` $array = array($j_name,$j_id,$j_address,$j_city,$j_zip,$j_district,$j_date,$j_ba,$j_type,$j_county); print_r($array);` – xXPhenom22Xx Feb 03 '12 at 20:52
  • So I guess my underlying issue is how to send back an arrays or pieces of data back to the AJAX script and in turn it would know its an array? – xXPhenom22Xx Feb 03 '12 at 20:55
  • 1
    Ok, I think the AJAX response you got is a simple string which you want to access as Javascript Array. I don't know PHP, but i think you should send the response as JSON instead of plain string.. if not.. you may end up writing a parser to convert the PHP array to Javascript array object. – Selvakumar Arumugam Feb 03 '12 at 20:55
  • So javascript is treating the returned info from the PHP script as a single line of text, hence the test[0] returning 'A' and test[1] returning 'r',etc... What is the ideal way to send multiple values to AJAX from php? in one call? – xXPhenom22Xx Feb 03 '12 at 20:57
  • check - "http://us3.php.net/manual/en/function.json-encode.php" - JSON for PHP. You should try using encode function to change it to a javascript array. – Selvakumar Arumugam Feb 03 '12 at 20:58
  • Thanks SKS for the help in realizing the 'array' I sent over from PHP was only being seen as a string of text. I decided to form the data sent in a way that it could be easily parsed in jQuery then displayed. Here is what I used in case anyone wants the snippet: var string = data; var partsArray = string.split('-'); Where each piece of data is seperated by a - in PHP and sent to the AJAX script Now I am able to .html(partsArray[0], etc.... and get the correct pieces Thanks again – xXPhenom22Xx Feb 03 '12 at 21:34
-1

var colors = ['#ff9e2a', '#2b908f', '#96c18c', "#ff3232", '#09a43e', '#7798BF', '#3ef3cd', '#ff0066',
  '#55BF3B', '#DF5353', '#7798BF', '#aaeeee', '#DDDF0D', '#058DC7', '#50B432', '#ED561B', '#DDDF00',
  '#24CBE5', '#90ee7e', '#FF9655', '#FFF263', '#6AF9C4', '#082a50', '#f7a35c', '#e9625e', '#dce405',
  '#a1034d', '#42A07B', '#9B5E4A', '#2e898e', '#82914E', '#7d7c7c', '#6ecfdf', '#f45b5b', '#aaeeee', '#ad4029', '#64E572', '#7cb5ec', '#8085e9', '#8d4654', '#514F78', '#72727F', '#1F949A', '#86777F', '#eeaaee', '#c62a2b'
]

$(document).ready(function() {

  for (i = 0; i < colors.length; i++) {
    console.log(colors[i]);
    $('#ColorArrayThemeA').append('<li class="bgclr' + i + '"><span>' + colors[i]     + '</span></li>');
    $('.bgclr' + i + '').css({
      "background-color": colors[i]
    });
    //$('#ColorArrayThemeA').css({ "background-color": colors[i] })


  }

});
ol#ColorArrayThemeA li {
  margin: 5px 20px;
  float: left;
  padding: 5px 0px;
  width: 20%;
  text-align: center;
  font-weight: bold;
  font-family: Arial;
}

ol#ColorArrayThemeA li span {
  background-color: white;
  padding: 2px 5px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ol id="ColorArrayThemeA"></ol>