I am an absolute newbie to jquery. So this might be a very basic question, please bear with me.
I have defined a jQuery function, which creates a 2-d array.
<script type="text/javascript">
$(document).ready(function() {
$.ajax({
url: '/testdata.txt',
dataType: 'text',
success: function(data) {
var datatable = [];
// Populate datatable[]. This is a 2-d array.
$('#myTestDiv').text(datatable[2][0]);
},
error: function(){
alert('error!');
}
})
});
</script>
<body>
<table>
<thead>
</thead>
</table>
</body>
Now, I want to print the 2-d array, “datatable”, in the HTML table, preferably with JSTL. But it appears that the “datatable” variable is not accessible outside . I know that the table is being populated correctly, the $('#myTestDiv').text(datatable[2][0]); is printing expected output.
How to achieve this?
Thanks a lot.
Remove
varfrom in front of the variable declaration. This will result in the variable being placed on the globalwindowobject, making it accessible from all over. Be careful with this type of practice though, as polluting your global context is frowned upon.