Recently I'm studying jQuery . I wish that when I write here
<input type="text" id="scrivo" />
I want to filter an array derived from a json file.
The json file is like this:
{
"Re":
[
{
"nm": "Edward the Elder",
"cty": "GB",
"hse": "House of Wessex",
"yrs": "899-925"
},
{
"nm": "Edgar",
"cty": "GB",
"hse": "House of Wessex",
"yrs": "959-975"
},
{
"nm": "Edward the Martyr",
"cty": "GB",
"hse": "House of Wessex",
"yrs": "975-978"
}
]
}
and the piece of code is this:
<script language="JavaScript">
$(document).ready(function()
{
$('#scrivo').keyup(function()
{
var json = (function () {
var json = null;
$.ajax({
'async': false,
'global': false,
'url': "re.json",
'dataType': "json",
'success': function (data) {
json = data;
}
});
return json.Re;
})();
var o = $.grep(json, function (n) { return (n.nm.indexOf("El")) },true);
});
});
</script>
For example I would like to filter the objects that contain "El", which is the first object in the json file. Instead I do not receive anything. Can you help me please?