Search code examples
javascriptphpjqueryhtmljsfiddle

append dropdown box selections to a textbox


I am wondering if someone can point me in the right direction for what I am looking for... Basically, I would like to have 3-5 dropdown boxes (and maybe 2 textboxes) with each their own specific options. I would then like to append (or as excel would call it, CONCATENATE) the selected options into a string of text in a textbox after pressing a button (call it generate). I could define the string of text and where the options would reside.

Crude example;

Selections:

Dropdown 1 - Dropdown 2 - Dropdown 3 - Textbox 1 - Textbox 2

Output in textbox:

Selection 1 - Selection 2 - Selection 3 - optional text - optional text

If anyone knows of an example that would best suit my needs that would be awesome so I can sort of figure it out... I have yet to come across one.


Solution

  • To achieve this, you would have to create a function that gathers all the values then formats them in the way you want.

    An example of this would be:

    function generate(){
        var result = '';
    
        result += document.getElementById('drop1').value + ' - ';
        result += document.getElementById('drop2').value + ' - ';
        result += document.getElementById('drop3').value + ' - ';
        result += document.getElementById('text1').value + ' - ';
        result += document.getElementById('text2').value;
    
    	document.getElementById('output').innerHTML = result;
    }
    generate();
    <select id="drop1" onchange="generate()">
        <option value="d1s1">D1 S1</option>
        <option value="d1s2">D1 S2</option>
    </select>
    <select id="drop2" onchange="generate()">
        <option value="d2s1">D2 S1</option>
        <option value="d2s2">D2 S2</option>
    </select>
    <select id="drop3" onchange="generate()">
        <option value="d3s1">D3 S1</option>
        <option value="d3s2">D3 S2</option>
    </select>
    <input id="text1" type="text" value="text1" onchange="generate()" onkeyup="generate()" />
    <input id="text2" type="text" value="text2" onchange="generate()" onkeyup="generate()" />
    
    <p id="output"></p>