0

Hi Guys I am ruby on rails developer and have no idea about javascript or Jquery.

I have a select tag like this :

<select name="form[city]" id="form_city">
   <option value="">Select Office</option>
   <option value="WA - Washington PD">WA - Washington PD</option>
   <option value="CA - California PD">CA - California PD</option>
   <option value="NY - NewYork PD">NY - NewYork PD</option>
</select>

When a user selects for example CA - California PD from the above select tag, the next select tag which will be below it should have a drop down list like this :

 <select name="form[cityselected]" id="form_cityselected">
   <option value="CAF">CAF</option>
   <option value="CAL">CAL</option>
   <option value="CAU">CAU</option>
   <option value="CAS">CAS</option>
</select>

Thus a Javascript or JQuery function has to be used to collect the option selected in "form[city]" and to append F,L,U,S characters to the first two characters from "form[city]" select tag to the second select tag -> form[cityselected].

Thanks a lot in advance

4 Answers 4

3

Check this : http://jsfiddle.net/Q8NW5/

$('#form_city').on('change', function (e) {
var val = $(':selected', $(this)).val(),
    val = val.split('-')[0],
    val = val.replace(/\s+/g, ''),
    char = ['F', 'L', 'U', 'S'],
    tpl = '<option val="{val}">{val}</option>',
    html = '';

$(char).each(function (item, value) {
    var opt = val + value;
    html += tpl.replace(/{val}/g, opt);
});

$('#form_cityselected').html(html).show();
$('#form_final_city').show();

var currCity = $('#form_cityselected option:selected').val();
setCity(currCity);

})

$('#form_cityselected').on('change', function () {
    var val = $(':selected', $(this)).val();
    setCity(val);
});

function setCity(city) {
    $('#form_final_city').val(city);
}
4
  • This worked perfectly fine for me thanks, is there a way to display the selected "form_cityselected" in a text field at the end
    – Supersonic
    Jan 22, 2013 at 10:26
  • 1
    I didn't get the question correctly. Is like this something you are looking for? jsfiddle.net/Q8NW5/1 Jan 22, 2013 at 10:51
  • Almost there, I need to output the selected second value i.e "form_cityselected" to a text field. The one you provided now outputs a label but the problem with that is it doesn't recognize the first selected value of the "form_cityselected" you need to manually select it till it displays on the label
    – Supersonic
    Jan 22, 2013 at 10:58
  • <input type="text" value="" name="form[final_city]" id="form_final_city"> is the input tag which should have the second selected value, so that the user can edit it later and save it.
    – Supersonic
    Jan 22, 2013 at 11:32
1

I haven't tested this, but this is the general idea... it's VERY specific to your exact question.

Notice the addition of onSelect in the select html... that's the only html I've changed

<select name="form[city]" id="form_city" onSelect="updateOtherSelect(this)">
       <option value="">Select Office</option>
       <option value="WA - Washington PD">WA - Washington PD</option>
       <option value="CA - California PD">CA - California PD</option>
       <option value="NY - NewYork PD">NY - NewYork PD</option>
    </select>

function updateOtherSelect(select){
   //grab just what happens before the first space in your option's value
   var prefix = select.options[select.selectedIndex].value.split(" ")[0];

   //go through all your second drop down values and append, based on the string FLUS (if I understood your question correctly).
   for(i=0; i< document.getItemById("form_cityselected").options.length && i < "FLUS".length; i++){
       document.getItemById("form_cityselected").options[i].value = prefix + "FLUS".charAt(i)
   }

}
1

try this

$('#form_city').change(function(){
    var cityselectedobj= $('#form_cityselected');
     cityselectedobj.empty()
    var val=$(this).val();
    var text=val.split(' ')[0];
    $('<option>').val(val + 'F').text(text+ 'F').appendTo(cityselectedobj);
    $('<option>').val(val + 'L').text(text+ 'L').appendTo(cityselectedobj);
    $('<option>').val(val + 'U').text(text+ 'U').appendTo(cityselectedobj);
    $('<option>').val(val + 'S').text(text+ 'S').appendTo(cityselectedobj);
)};

fiddle here

1
  • 1
    you're forgetting to parse out just the chars before the space in his initial value Jan 22, 2013 at 7:54
1

Below is the example : I am supposing you know the basics of Jquery .

$("#form_city").change(function() {
     // TODO 
filltheVillagecomboBox(villageStrings);//village strings seperatedBy '|'

 });


function filltheVillagecomboBox(villagesStrings){
            if(villagesStrings !=null){
                var villagesStrings = villagesStrings.split('|');

                box.append("<option value='null'>Please select your village</option>");

                for (var i = 0, l = villagesStrings.length-1; i <l; i++){
                    var id =villagesStrings[i].substring(0,2);
                    var village=villagesStrings[i].substring(0,villagesStrings[i].length)
                    $("#form_village").append("<option value="+village+">"+village+"</option>");
            }
            }

            }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.