Click here to Skip to main content
15,917,005 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
I am having two combo boxes in my html page. Based on the value selected in first combo box value I need to load data in second combo box value.(EX - Country and States combo boxes). I am able to populate the values in second combobox properly using Java Script. But my problem is after populating values in 2nd combobox , when I am selecting the value in 2nd combobox , that is not displayed immideatly. Still 2nd combobox is having BLUE empty space. It is not binding the selected value.

But when I change the value in first COmbo box,then the previous value selected in 2nd combo box is getting displayed. I tried many things but dint get any solution. Its very urjent.


Thanks in Advance.
Posted
Updated 28-Dec-17 23:44pm
Comments
Christian Graus 27-Jul-12 3:50am    
Don't worry, I deleted where you (rudely) asked this twice.
Naveen_143 27-Jul-12 4:50am    
It was not posted twise intensionally. Due to some network problem it happend.
Naveen_143 9-Aug-12 3:12am    
Thanks for deleting. I was trying to do that.

This is not 'urjent' to us. It's not your right to hassle us to do your job for you, faster.

You can't do this in Javascript and HTML only. Are you using ASP.NET ? In ASP.NET, the page load event fires before your event, and the page prerender fires after. So if you populate your lists in page load, your event is firing and changing the selected index, AFTER you loaded the data. Move that code to prerender.

This is a wild guess based on your incomplete post. Clarify and post code if I am wrong.
 
Share this answer
 
Comments
Naveen_143 27-Jul-12 4:48am    
Thanks for your reply. I am not using ASP.Net. Its only HTML pages and java scripts.
Naveen_143 27-Jul-12 4:49am    
OnChange function of the first combobox, I am populating the values in second combobox.
Christian Graus 27-Jul-12 5:22am    
Why is it urgent, if you're doing it this way ? Why not post your code ?
Before I start, I'm going to echo that which Christian said earlier. It's not very 'urjent' to us at all. This has been combined with a 'thanks in advance' - which may be interpreted by some as an indication that any effort will not be acknowledged after the fact.

For me, the last two sentences of your question spoiled what would otherwise be a-ok. "it's very urjent. Thanks in advance"

Anyway, this isn't terribly difficult. Visit http://www.w3schools.com/js/default.asp[^] often, consider it roughly equivalent to MSDN for all things web-related, though admittedly it's not as comprehensive.

. Try the following:
XML
<!DOCTYPE html>
<html>
  <head>
  <script>
    function byId(e) {return document.getElementById(e);}

    function stateComboChange()
    {
        var combo1 = byId('stateCombo');
        var combo2 = byId('cityCombo');
//      alert(combo1.value);

        emptyCombo(combo2);
        switch(combo1.value)
        {
            case '-1':  addOption(combo2, -1, '-select state first-');
                        break;
            case '0':       addOption(combo2, 0, 'Melbourne');
                        addOption(combo2, 1, 'Horsham');
                        break;
            case '1':       addOption(combo2, 2, 'Sydney');
                        addOption(combo2, 3, 'Bondi');
                        break;
            case '2':       addOption(combo2, 4, 'Hobart');
                        addOption(combo2, 5, 'Port Arthur');
                        break;
        }
        cityComboChange();
    }

    function cityComboChange()
    {
        var combo2, tgt;
        combo2 = byId('cityCombo');
        tgt = byId('tgt');

        tgt.innerHTML = combo2.options[combo2.options.selectedIndex].title;
    }

    function emptyCombo(e)
    {
        e.innerHTML = '';
    }

    function addOption(combo, val, txt)
    {
        var option = document.createElement('option');
        option.value = val;
        option.title = txt;
        option.appendChild(document.createTextNode(txt));
        combo.appendChild(option);
    }

  </script>
  </head>
  <body>
    <select id='stateCombo' onchange='stateComboChange();'>
        <option value='-1' title='-select one-'>-select one-</option>
        <option value='0' title='Vic'>Vic</option>
        <option value='1' title='Nsw'>Nsw</option>
        <option value='2' title='Tas'>Tas</option>
    </select>

    <select id='cityCombo' onchange='cityComboChange();'>
        <option value='-1' title='-select state first-'>-select state first-</option>
    </select>

    <div id='tgt'></div>
</body>
</html>


Edited to add display of city list selected item's text.
 
Share this answer
 
v2
Comments
Naveen_143 27-Jul-12 7:36am    
Hi..thanks for your reply. Its nice. I have a doubt, can you tell me how to bind the slected value to the combo box in the javascript.
Rockingadget 13-Dec-13 2:50am    
Hello,

Many thanks to enhzflep for this nice code. Help me a lot!
Is it possible to have also a code for "cathing" the selected values and via a submit button to send them over email?

Best Regards

Andreas Achilleos
enhzflep 27-Jul-12 7:58am    
Certainly, as soon as I understand what you mean. What do you mean by that?
I'm familiar with the term when the control is represented as a control object and the input data is represented as a list object.
E.g Flex, .NET, M$ Acess... never seen it used in this context.

So what do you mean, without using the word bind?
enhzflep 14-Dec-13 8:39am    
You're welcome Andreas. Haven't done any email submission stuff for years myself and would have to research myself to be sure, though I suppose you could use the values gleaned from the combo-boxes to construct a mailto type of url. A url that would open the default mail program on the client machine with the TO: TITLE: and BODY: fields pre-populated. Failing that, you could just catch the values on the server-side and use a server-side email solution.
Naveen_143 27-Jul-12 8:19am    
I mean..when I am selecting a value in combo box,it is not getting displayed. Only BLUE empty field is displaying. How to display the selected value of the combo box.?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900