Jquery, XML, Loop in different condition

ITsolutionWizard
ITsolutionWizard used Ask the Experts™
on
Below codes are working fine except
 
htmlOption += "<option>" + AmountMax + "</option>";

it only return 1 record/row when bondName='DMV Traffsdfasdfads', how can I loop the <Amount></Amount> and get them into the dropdown in HTML?


<Surety>
  <SuretyLine>
    <Commercial>
	<Bond>
        <GeneralInformation UI="BondInfo">
          <Name ID="bondName" PrefillValue="Yes">DMV Traffic Violators School Classroom</Name>
          <State ID="bondState" PrefillValue="Yes">CA</State>          
         </GeneralInformation>
        <Coverage Type="List">
          <Amount  PrefillValue="No" Min="10000" Max="10000" Year="1">10000</Amount>
         </Coverage>              
      </Bond>
      <Bond>
        <GeneralInformation UI="BondInfo">
          <Name ID="bondName" PrefillValue="Yes">DMV Traffsdfasdfads</Name>
          <State ID="bondState" PrefillValue="Yes">CA</State>          
         </GeneralInformation>
        <Coverage Type="List">
          <Amount  PrefillValue="No" Min="10000" Max="20000" Year="1"></Amount>
	  <Amount  PrefillValue="No" Min="20000" Max="30000" Year="1"></Amount>
         </Coverage>              
      </Bond>
</Commercial>
  </SuretyLine>
</Surety>

function fnGetBondAmount() {
    $.ajax({
        url: "http://localhost:5489/BondList.xml",
        success: function (xml) {
            parseSelectXMLBondAmountList(xml, "bondAmount", localStorage.getItem('bondState'), localStorage.getItem('bondName'))
        },
        error: function (xhr, ajaxOptions, thrownError) {
            alert("Status: " + xhr.status);
            alert("Error: " + thrownError);
        }
    });
} 
function parseSelectXMLBondAmountList(xml, selectid, bondState, bondName) {
    var html = "";
    var htmlOption = "";
    
    $('#' + selectid).prop("disabled", false);   
    $(xml).find('Bond').each(function (i, e) {
        {
            if (bondState == $(e).find('GeneralInformation').find('State').text() && bondName == $(e).find('GeneralInformation').find('Name').text())
            {
                 
                var selectedBondName = $(e).find('GeneralInformation').find('Name').text();
                var coverageType = $(e).find('Coverage').attr('Type'); 
                if (coverageType == "list") {
                    var AmountMax = $(e).find('Amount').attr('Max');
                    var AmountMin = $(e).find('Amount').attr('Min');                   
                    htmlOption += "<option>" + AmountMax + "</option>";
                }
                else {
                    var AmountMax = $(e).find('Amount').attr('Max');
                    var AmountMin = $(e).find('Amount').attr('Min');
                    $('#bondAmountPlaceHolder').append("<input type='text'  id='bondAmount' name='bondAmount' value='" + AmountMax + " />");                  
                }
            }
        }
    });   
    $("#" + selectid).html(htmlOption);
} 

<select disabled class="select2" id="bondAmount" name="bondAmount" data-placeholder=""></select>  

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Three things.
1. JavaScript string comparison is case sensitive. The XML element Coverage has the attribute Type with the value "List" which is uppercased, but you are comparing the value with lower cased "list" (in your line 51)
2. You are iterating Bonds, but not Amounts. You need iterate them same way, like:
                if (coverageType == "List") {
                   $(e).find('Amount').each(function (i, a) {
                    var AmountMax = $(a).attr('Max');
                    var AmountMin = $(a).attr('Min');
                    htmlOption += "<option>" + AmountMax + "</option>";
                   });
                }

Open in new window

3. Would be better to cache the function results for performance sake, don't call the same functions over and over again to obtain the same value.

Author

Commented:
It returns undefined.

Author

Commented:
It works. Expert :-D

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial