Reputation: 3664
I have a json data in following format
{"Jack":["[email protected]","US"],"Rob":["[email protected]","UK"]}
i have a select tag, if the value selected from a select box equal to "Jack
" then it should print the data "[email protected]","US"
select box on change function is
function printDataOf(val){
now if selectd value is Jack
alert() // should alert [email protected]
alert() // should alert US
how do i do it?
Upvotes: 0
Views: 2206
Reputation: 16139
The onchange function will not be able to receive the actual value. You can pass the select object (as 'this') though.
<select onchange="printDataOf(this)">
<option/>
<option value="Jack">Jack</option>
<option value="Rob">Rob</option>
</select>
Now in the handler you can get the actual selected value and display the desired data.
var json_obj = {"Jack":["[email protected]","US"],"Rob":["[email protected]","UK"]};
function printDataOf(val){
var Mval = $(val).val();
alert(json_obj[Mval][0]);
alert(json_obj[Mval][1]);
}
It would be wise to check for existence of Mval in the json object. If it is not there you will receive an error. I didn't do it here to keep it simple.
Here is a working example.
Upvotes: 1
Reputation: 10724
Try this.
var data = {"Jack":["[email protected]","US"],"Rob":["[email protected]","UK"]}
var selecteVal="Jack";
if(typeof(data[selecteVal])!='undefined'){
alert(data[selecteVal][0]);//[email protected]
alert(data[selecteVal][1]);//US
}
Upvotes: 0
Reputation: 447
Loop Json data and then compare and print . Using jquery...
var data = {"Jack":["[email protected]","US"],"Rob":["[email protected]","UK"]}
var selecteVal="Jack";
$.each(data,function(key,index){
if (key==selecteVal){
alert(data[key][0])
alert(data[key][1])
}
});
See JSFiddle
Upvotes: 0
Reputation: 1373
This should do it;
printDataOf('Jack');
function printDataOf(val) {
var data = '{"Jack":["[email protected]","US"],"Rob":["[email protected]","UK"]}';
var match = JSON.parse(data)[val];
var email = match[0];
var country = match[1];
alert(email);
alert(country);
}
Upvotes: 1
Reputation: 919
This should get you what you needed,
var result = {"Jack":["[email protected]","US"],"Rob":["[email protected]","UK"]};
function printDataOf(val){
var data = JSON.parse(result);
var selected = data[val];
var selected_arr = selected.split(',');
//now if selectd value is Jack
alert(selected_arr[0]) // should alert [email protected]
alert(selected_arr[1]) // should alert US
}
Upvotes: 0
Reputation: 21
for (key in jsonobject) {
alert(jsonobject[val][0];
alert(jsonobject[val][1];
alert(jsonobject[val][2];
}
Upvotes: 0
Reputation: 9034
Try this, change json
with your actual object:
function printDataOf(val)
{
for (name in json)
{
if (name == val)
{
alert(json.name[0]);
alert(json.name[1]);
}
}
}
Upvotes: 0
Reputation: 3485
This should work.
function check( json_obj )
{
return json_obj[0] == 'Jack'
}
if you're using jquery, also try this:
json_obj = jQuery.parseJSON( json_obj.responseText );
To examine what you really got, use firebug's debugger
Upvotes: 0