1

Here is my JavaScript with Ajax code:

Actually i am using this for dynamically adding option in any number of select in which this function is called.

function loadabc(vm) {
    var xmlhttp;    
    //alert(vm);

    if (window.XMLHttpRequest) {
        // code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    }
    else {
        // code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }

    //alert("called");
    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4) {
            var jsonObj = JSON.parse(xmlhttp.responseText);
            for(i = 0; i < jsonObj.length; i++) {
                var createOption = document.createElement("option");
                //alert("Jeason has Passed Data");
                createOption.value = jsonObj[i].aId;
                createOption.text = jsonObj[i].aName;
                //alert("id" + createOption.value);
                //alert("Name" + createOption.text);
                document.impForm.vm.options.add(createOption);
                //alert("Added");
            }
        }
    }
    xmlhttp.open("get", "${pageContext.request.contextPath}/Admin_Search_con?flag=loaddetail", true);
    xmlhttp.send();
}

I am using ondblclick="loadabc(this)" for calling it. I want to access this vm object for creating options in select. How can I do so?

3
  • 1
    Don't understand well what the problem is. Do you gen an error when trying to access vm within... where? Commented Mar 16, 2015 at 9:49
  • When i am passing this object in function , in alert it is displaying but when i am using it for document.impForm.vm.options.add(createOption) options are not created. Commented Mar 17, 2015 at 4:37
  • What does happen if you do vm.options.add(createOption) instead of document.impForm.vm? Commented Mar 17, 2015 at 8:37

1 Answer 1

1

The solution is to use the param vm instead of referencing it as document.impForm.vm:

function loadabc(vm) {
  var xmlhttp;    
  //alert(vm);

  if (window.XMLHttpRequest) {
      // code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp = new XMLHttpRequest();
  }
  else {
      // code for IE6, IE5
      xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  }

  //alert("called");
  xmlhttp.onreadystatechange = function() {
      if (xmlhttp.readyState == 4) {
          var jsonObj = JSON.parse(xmlhttp.responseText);
          for(i = 0; i < jsonObj.length; i++) {
              var createOption = document.createElement("option");
              //alert("Jeason has Passed Data");
              createOption.value = jsonObj[i].aId;
              createOption.text = jsonObj[i].aName;
              //alert("id" + createOption.value);
              //alert("Name" + createOption.text);
              vm.options.add(createOption); // <-- Here!
              //alert("Added");
          }
      }
  }
  xmlhttp.open("get",  "${pageContext.request.contextPath}/Admin_Search_con?flag=loaddetail", true);
  xmlhttp.send();
}
Sign up to request clarification or add additional context in comments.

Comments

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.