Jquery - Passing selected row values to a hidden text box

Jquery - Passing selected row values to a hidden text box

I'm working on a postcode (airports & train station) search but can't seem to figure out why the selected row values are not writing the correct values to the hidden text boxes.

Basically if I search 'Gatwick' as shown below:



I get the following XML response:

  1. <?xml version="1.0"?>
    <addresslist resultcount="4">
       
    <address id="0" catagoryid="1" lat="184179968" lng="-639296">
           
    <companyname></companyname>
           
    <premiseno></premiseno>
           
    <streetname>GATWICK AIRPORT (NORTH)</streetname>
           
    <townname></townname>
           
    <postcode>RH6 0PJ</postcode>
       
    </address>
       
    <address id="1" catagoryid="1" lat="184161536" lng="-586944">
           
    <companyname></companyname>
           
    <premiseno></premiseno>
           
    <streetname>GATWICK AIRPORT (SOUTH)</streetname>
           
    <townname></townname>
           
    <postcode>RH6 0NP</postcode>
       
    </address>
       
    <address id="2" catagoryid="1" lat="184161664" lng="-580224">
           
    <companyname></companyname>
           
    <premiseno></premiseno>
           
    <streetname>GATWICK GATWICK AIRPORT RAILWAY STATION</streetname>
           
    <townname></townname>
           
    <postcode>RH6 0RD</postcode>
       
    </address>
       
    <address id="3" catagoryid="1" lat="184161536" lng="-586944">
           
    <companyname></companyname>
           
    <premiseno></premiseno>
           
    <streetname>GATWICK RAILWAY CONCOURSE</streetname>
           
    <townname></townname>
           
    <postcode>RH6 0NN</postcode>
       
    </address>
    </addresslist>

At the moment, no matter which row I select, it always writes the last Lat & Long values to the hidden textbox. For example, If i select the first row, it will write 184161536, -586944 which is the Lat Long values for 'GATWICK RAILWAY CONCOURSE'

Here is the Jquery Code:

  1. function buildResultView(xml_object,pageNum,rowdiv) {

    // Remove existing page up page down click events
    rowdiv.find('.lbl-addr-pgup').unbind('click');
    rowdiv.find('.lbl-addr-pgdn').unbind('click');

    // Remove result list if there is one
    if (rowdiv.find('.ul-result-view')) {
        rowdiv.find('.ul-result-view').remove();
    }

    ul = $('










    <ul></ul>');
    ul.addClass('ul-addr-res');
    ul.addClass('ul-result-view');

    // Prepend ul before resultinfo
    rowdiv.find('.div-result-info').before(ul);

    // Reset result count
    var resCount = 0;

    // Count Results
    $(xml_object).find('address').each(function(){
        resCount += 1;
    });

    // Pull out start row and max rows
    var pageRes = clientPaginate(resCount,pageNum).split(',');
    var loopMax = parseInt(pageRes[0]);
    var currentRow = parseInt(pageRes[1]);
    var lastPge = parseInt(pageRes[2]);
    var addType;

    // Show maxRows starting at startIndex  
    for (var i = currentRow; i < loopMax; i++) {

        var li = $('
























    <li></li>');
        li.addClass('li-addr-res');
        addType = parseInt($(xml_object).find("address[id='"+i+"']").attr("catagoryid"));
        var lat = ($(xml_object).find("address[id='"+i+"']").attr("lat"));
        var lng = ($(xml_object).find("address[id='"+i+"']").attr("lng"));

        li.css('cursor','pointer');

    // Reset result count
    var resCount = 0;

    // Count Results
    $(xml_object).find('address').each(function(){
        resCount += 1;
    });

    // Pull out start row and max rows
    var pageRes = clientPaginate(resCount,pageNum).split(',');
    var loopMax = parseInt(pageRes[0]);
    var currentRow = parseInt(pageRes[1]);
    var lastPge = parseInt(pageRes[2]);
    var addType;

    // Show maxRows starting at startIndex  
    for (var i = currentRow; i < loopMax; i++) {

        var li = $('

























    <li></li>');
        li.addClass('li-addr-res');
        addType = parseInt($(xml_object).find("address[id='"+i+"']").attr("catagoryid"));
        var lat = ($(xml_object).find("address[id='"+i+"']").attr("lat"));
        var lng = ($(xml_object).find("address[id='"+i+"']").attr("lng"));




Then I write the lat, lng values to the hidden textbox:

  1.     // Add this lat lng to hidden text box
        rowdiv.find('.hidden-lat-lng').val(lat+","+lng);

Any idea why this isn't selected the correct latitude longtitude values?

I just need to be able to get the latitude longtitude values of the row that is selected.

If anybody could help with this, I would be very greatful! :-)