Finding the id of a jquery autocomplete input from within the onItemSelect call

Discussion in 'jQuery' started by DigitalFTW, Nov 18, 2009.

  1. #1
    i have this,

    
    $(function() {
           $('#input').autocomplete('test.php', {json: true});
         });
    
    Code (markup):
    that bring back values from the database here is my php section

    
    $result = mysqli_query($db,$query);
    
    while( $row = mysqli_fetch_array($result) )
    {	
    $aResults[] = array( "id"=>($row['s_id']) ,"value"=>($row['s_last']), "info"=>($row['s_first']) );
    }
    	
    $return = array();
    
    foreach ($aResults as $key => $res) {
    	 $return[] =$res['value'] . ' , ' . $res['info'];
    	 
    }
    
    
    echo json_encode($return);
    
    
    
    		}
    	}
    }
    
    Code (markup):
    i would like, when i select a name from the auto suggest to be able to have the id be filled in a input text area...

    uncle Google says this, but i am having a hard time implementing it.

    
    myCallback = function(li, $input) {
        // I need to refer to the appropriate "myXxxInput" here
        alert($input.attr('id'));
    }
    
    setup = function() {
        setupInput($('#myFirstInput'));
        setupInput($('#mySecondInput'));
    }
    
    function setupInput($input) {
        $input.autocomplete('blah.php', {onItemSelect: function(li) {
            myCallback(li, $input);} });
    }
    
    
    Code (markup):
     
    DigitalFTW, Nov 18, 2009 IP