2

am trying to set listbox values as selected based on the value obtained from json

My problem is while putting static data am getting that values as selected.

please look at this http://jsfiddle.net/9Stxb/89/ But in the above code if my json data change, the above selection will not correct.this will be sometimes val1,val2,val3 or any combination .

i used following code

but it is not working.. http://jsfiddle.net/9Stxb/91/

code

var myData ={
    "_id": "525f8226360cc02c2bd63dec",
    "id": "30",
    "jobs": [{
        "_id": "524a4f4cc973602da0d4ee10",
        "id": "4",
        "launch": "mypraram val url",
        "names": "val1",

    }, {
        "_id": "525f6deb360cc02c2bd63dea",
        "id": "28",
        "launch": "jithinurl",
        "names": "val2"
    }],
    "server": "180.16.17.60"
} 
$(function() {
    for(x in mydata.jobs){        $('#jobSel').val(['mydata.jobs[x].names','mydata.jobs[x].names']);
   } 
});

html

<div class="id_100">
    <select id="jobSel" class="longcombo" multiple>
        <option value="val1">Val 1</option>
    <option value="val2">Val 2</option>
    <option value="val3">Val 3</option>
    </select>
</div>
2
  • Please also put the relevant code in your question. Commented Oct 17, 2013 at 9:38
  • @Psl posted answer ...have a look Commented Oct 17, 2013 at 9:45

1 Answer 1

2

Here's updated js...

Demo Fiddle

var jsonData = {
    "_id": "525f8226360cc02c2bd63dec",
        "id": "30",
        "jobs": [{
        "_id": "524a4f4cc973602da0d4ee10",
            "id": "4",
            "launch": "mypraram val url",
            "names": "val1",

    }, {
        "_id": "525f6deb360cc02c2bd63dea",
            "id": "28",
            "launch": "jithinurl",
            "names": "val2"
    }],
        "server": "180.16.17.60"
}
$(function () {
    var selectedVals = new Array();

    for (var i=0;i<jsonData.jobs.length;i++) {
        console.log(jsonData.jobs[i])
        selectedVals.push(jsonData.jobs[i].names);
    }
    $('#jobSel').val(selectedVals);
});
Sign up to request clarification or add additional context in comments.

2 Comments

In this case, you can either match it with select option's text value or can extract id from "names" and use push it in array.
how can i ge the id of names?please help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.