Link to home
Start Free TrialLog in
Avatar of ITsolutionWizard
ITsolutionWizardFlag for United States of America

asked on

jquery xml

For example, if i want to get <Option></Option> Value with condition: state="AZ" and Code - "939"
in Jquery. I do not understand why below codes are not working.


<script type="text/javascript">
      function GetBondAmount(selectedCode, selectedState) {
          //alert("Value: " + selectedCode + " " + selectedState);
          //return;
          var xml = "data/bond.xml";
          var select = $('#bondAmountList');
          $(document).ready(function () {
              $.ajax({
                  type: "GET", url: xml, dataType: "xml",
                  success: function (xml) {
                      var select = $('#bondAmount');
                      //select.append('<option value="">Select Bond Type</option>');
                      $(xml).find('Bond').each(function () {
                          var name = $(this).find('Name').text();
                          var code = $(this).find('Code').text();
                          var state = $(this).find('State').text();                          
                          if ((state == selectedState) && (code == selectedCode))
                          {
                              var bondAmountType = $(this).find('BondAmountType').text();
                              if (bondAmountType == "List") {
                                  //get the list
                                  $(xml).find('BondAmount').each(function () {
                                      var option = $(this).find('Option').text();
                                      alert(option + "\n");
                                  });
                              }
                           }
                       });
                  }
              })
          });
      }
  </script>



<?xml version="1.0" encoding="utf-8" ?>
<Bonds>
  <Bond>
    <Name>Defective Title Bond</Name>
    <Code>939</Code>
    <Status>Active</Status>
    <State>AZ</State>
    <BondAmountType>Fix</BondAmountType>
    <BondAmount>
      <Option>1000</Option>
    </BondAmount>
  </Bond>
  <Bond>
    <Name>Defective Title Bond</Name>
    <Code>939</Code>
    <Status>Active</Status>
    <State>CA</State>
    <BondAmountType>List</BondAmountType>
    <BondAmount>
      <Option>100</Option>
      <Option>200</Option>
    </BondAmount>
  </Bond>  
  <Bond>
    <Name>Janitoral Business Bond</Name>
    <Code>111</Code>
    <Status>Active</Status>
    <State>CA</State>
    <BondAmountType>List</BondAmountType>
    <BondAmount>
      <Option>100</Option>
      <Option>200</Option>
    </BondAmount>
  </Bond>
</Bonds>
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

I don't see a <data> element anywhere, but you are using "data/bond.xml".  Are you expecting JQuery to load the XML from a file or from the DOM?
Avatar of ITsolutionWizard

ASKER

yes
Oops, got confused because the variable that you are using outside of the success function, is the same as the variable inside of the success function.

Does this expression return an element (I don't see an element with id='bondAmount')?

       var select = $('#bondAmount');
ASKER CERTIFIED SOLUTION
Avatar of louisfr
louisfr

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial