IsaR
IsaR

Reputation: 17

Option Value: Select One, Output Multiple

The question I have this bit of code:

<FORM NAME=formname>
<SELECT NAME=name1 onchange=results(1)>
<Option VALUE="" selected>Select one:
<Option VALUE="OPTION A">OPTION A
<Option VALUE="OPTION B">OPTION B
<Option VALUE="OPTION C">OPTION C</SELECT>
<SELECT NAME=name2 onchange=results(1)>
<Option VALUE="" selected>Select one:
<Option VALUE="OPTION D">OPTION D
<Option VALUE="OPTION E">OPTION E
<Option VALUE="OPTION F">OPTION F</SELECT>

Which lets me build sentences for wide varieties of subjects if I add enough variables.

The results are displayed here:

<TEXTAREA NAME=textarea COLS=110 ROWS=7>[Selected description shows up here]</TEXTAREA></FORM>

With this javascript:

<SCRIPT LANGUAGE=JAVASCRIPT TYPE="TEXT/JAVASCRIPT">
function results(s_option)
{ with (document.forms["formname"])
{ if (name1.value != 'blank')
{ textarea.value = name1.value + name2.value ;} 
} 
} 
</SCRIPT>

I would like to add multiple option values, and for the first textarea to display all first values, and the second box to display all second values.

Where I'm lost:

<Option VALUE="OPTION A">OPTION A

Is there a way to do something similar to

<Option VALUE="OPTION A1 | OPTION A2 | OPTION A3 | OPTION A4">OPTION A

And have four textareas, each displaying their own version of OPTION A (A1, A2, A3, A4) into each textarea, all by only selecting "OPTION A"?

Upvotes: 0

Views: 75

Answers (1)

Barmar
Barmar

Reputation: 781741

You can use the split() method to split up the value.

$("select").change(function() {
    var option1 = $("select[name=name1]").val().split(" | ");
    var option2 = $("select[name=name1]").val().split(" | ");
    var limit = Math.max(option1.length, option2.length);
    for (var i = 0; i < limit; i++) {
        var val1 = i < option1.length ? option1[i] : '';
        var val2 = i < option2.length ? option2[i] : '';
        $("textarea").eq(i).val(val1 + val2);
    }
});

Upvotes: 1

Related Questions