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,

6
  • hmmm.. try .html( "" + test[0] ) ? Commented Feb 3, 2012 at 20:34
  • That's not how you build an array in JS Commented Feb 3, 2012 at 20:34
  • Need more content, where is test defined? Also what is Array ( [0] => 2820 Prairie [1] => 19316 [2] => 2820.... supposed to be? Commented Feb 3, 2012 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; Commented Feb 3, 2012 at 20:41
  • And this array is made up of an address and some other details about a given job. Commented Feb 3, 2012 at 20:44

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

7 Comments

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"
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);`
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?
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.
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?
|
-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>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.