0

I have two jquery comboboxes populated with various options:

<select id="project">
    <option value="1">This is project 1</option>
    <option value="2">This is project 2</option>
</select>
<select id="tasks">
     <option value="1">This is task 1</option>
     <option value="2">This is task 2</option>
</select>

I haven't shown all the html that the comobobox script adds which includes text boxes. The problem is when I make a selection from the first combo I want to script a selection in the 2nd comobobox, but for some reason I can't get the selection to show.

I would create a jsfiddle to demostrate the problem but I don't know how to link resources.

I've tried setting the selectedIndex member or calling the val() method, no luck.

nicael
  • 18,550
  • 13
  • 57
  • 90
SPlatten
  • 5,334
  • 11
  • 57
  • 128

3 Answers3

1

i think this is what you need

let's say you need to change task value to be like project value

$('#project').change(function() {
    $('#tasks').val('here_is_the_related_second_value');
});

UPDATE 1

could you try this:

$("#project").combobox({ 
    select: function (event, ui) { 
        $('#tasks').val('here_is_the_related_second_value');
    } 
});

See This

Alaa M. Jaddou
  • 1,180
  • 1
  • 11
  • 30
  • Sorry no that doesn't work, and using the combobox I do not get the change event. – SPlatten Dec 23 '15 at 19:14
  • @SPlatten can you explaine what exactly the functionality will be ? i mean you need to type into a combobox and when typing change the value is that what you need ? – Alaa M. Jaddou Dec 23 '15 at 19:16
  • What i want to do is make a selection from the first combo, then according to the selection lookup the task from an internal object and set the task, updating the second combo to display the task. – SPlatten Dec 23 '15 at 19:22
  • @SPlatten could you please give me how the object look like so i can make a solution please – Alaa M. Jaddou Dec 23 '15 at 19:24
  • Yes, thats what I'm doing and it works for the first combo, however my problem is that I have a second combo and I want to set the value in the second combo when the first is changed. – SPlatten Dec 23 '15 at 19:24
  • @SPlatten Please could you see the update of my answer – Alaa M. Jaddou Dec 23 '15 at 19:32
  • If I knew my way around jsFiddle better I would post a link with a demo to show off the problem, but I'm not sure how to link scripts on jsFiddle. Sorry, I just read your answer again, I will try and post when I have the result. – SPlatten Dec 23 '15 at 19:39
  • Unforunately whilst this might set the selected element, the – SPlatten Dec 23 '15 at 19:47
  • 1
    Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/98852/discussion-between-alaa-m-jaddou-and-splatten). – Alaa M. Jaddou Dec 23 '15 at 19:52
1

My proposal is:

$(function () {
  $('#project').change(function(e){
    var itemToSelect = $(this).find(":selected").val();
    $('#tasks').val(itemToSelect);
  });
});
<script src="//code.jquery.com/jquery-1.11.3.js"></script>

<select id="project">
    <option value="1">This is project 1</option>
    <option value="2">This is project 2</option>
</select>
<select id="tasks">
    <option value="1">This is task 1</option>
    <option value="2">This is task 2</option>
</select>
gaetanoM
  • 41,594
  • 6
  • 42
  • 61
0

The solution as discussed with "Alla M. Jaddou" was to add an id to the input box created by the $.widget( "custom.combobox", {}); callback.

In the createAutocomplete method I get the id assigned to the select element:

    id = this.element[0].id;

I then add an id attribute to the input box with the prefix "inp".

When a selection is changed in the combo I then get the input element and set the text.

SPlatten
  • 5,334
  • 11
  • 57
  • 128