gumuruh
gumuruh

Reputation: 93

jquery : autocomplete returned Name(s) and ID(s)?

Actually I have this html interface;

<input type="text" size="20" name="brandname" />
<input type="hidden" name="brandid" value="" />

Actually above there is a hidden input value namely as "brandid".

And below is the jquery;

$('#brandname').autocomplete("searchbrandnames.jsp", {
            minChars: 3
        });

OK... the ideas here is that; Once the user type something into that textfield, the jquery will work to have a somekind of autocomplete... like what google does for example.

But here, if we notice from the interface above, There is a hidden value there, Green colored.

That hidden value is going to be used as a hidden variable stored. That one, that made me question this thread.

What is the best way to obtain autocomplete into the textfield and also by getting the id into the hidden value as well? In above example; let's assume,

  1. we are typing a brand name into the textfield,
  2. then jquery request into the db for some similar brand names,
  3. it returned their names with their id; respectively 1 to the textfield and another 1 into the hidden value.

But, as far as I'm working with jquery right now, many plugins available there... just returned values into a single place nor many values into many places. I'm saying places means, referring to the html elements.

Hmmm.... is there any turn out for this case?

Upvotes: 1

Views: 3012

Answers (1)

Phil
Phil

Reputation: 164734

Assuming you're using the Autocomplete from jQuery UI

$('#brandname').autocomplete({
    source: 'searchbrandnames.jsp',
    minChars: 3,
    select: function(event, ui) {
        $('#brandname').val(ui.item.value);
        $('#brandid').val(ui.item.id)
    },
    search: function() {
        $('#brandid').val('');
    }
});

The value and id item properties will differ depending on the data returned by your JSP file.

See http://jqueryui.com/demos/autocomplete/#custom-data

Upvotes: 1

Related Questions