1

I have something like this:

<select class="bla">
  <option value="1">...</option>
  <option value="2">...</option>
</select>

<select class="bla">
  <option value="1">...</option>
  <option value="2">...</option>
</select>

<select class="bla">
  <option value="1">...</option>
  <option value="2">...</option>
</select>

<input class="alloptions" type="hidden" value="">

I want the hidden input field value to change every time a different option is selected in each of the select input fields above. This value would contain the selected options from all input fields separated with commas.

How can I do this?

13
  • You'll find it much easier (and quicker to boot) if you used identifiers rather than classes. Commented Jul 7, 2010 at 13:22
  • I want the hidden input field value to change every time a different option is selected in each of the select input fields above. i don't think you do. what problem are you trying to solve? Commented Jul 7, 2010 at 13:22
  • well I need a certain option inside a wordpress widget. the problem is that this option needs to be a array, so I need a input field that simulates the array by taking all select values separated with commas. then in the backend I use explode to create a array from this string. Commented Jul 7, 2010 at 13:35
  • @Alex: So the form is actually send to the backend and processed there? Why not just extract the values of the select fields? Like $config = array($_POST['optionA'], $_POST['optionB'], $_POST['optionC']) ? Commented Jul 7, 2010 at 13:38
  • 1
    @Alex: I am not sure if WP allows arrays. But you can test it. You are right, if the select elements have the same name, then only the last one will make it. To make PHP treat them as array, you have to append [] to the name, i.e. <select name="option[]"></select><select name="option[]"></select>... See also: php.net/manual/en/language.variables.external.php around example 3 Commented Jul 7, 2010 at 13:54

4 Answers 4

4

Something like:

$('select.bla').change(function() {
    $value = $('select.bla').map(function(){return $(this).val()}).get().join(',');
    $('input.alloptions').val($value);
});

Explanation:

  • change() gets fired whenever the value of a select field changes
  • With map() we create an array of the values of the select fields and join them to a string separated by commas
Sign up to request clarification or add additional context in comments.

Comments

0

change event is what you are after.

Comments

0

The .serialize() method can act on a jQuery object that has selected individual form elements, such as "input", "textarea", and "select" - meaning you could do the following:

$('.bla').change(function(){
   var opts = $('select').serialize();
   $('.alloptions').val(opts);
});

Each "select" would need a name value and would produce:

selec1=1&select2=2&select3=1

Comments

0

Do you really need to use Javascript for that? If you give all three select boxes the same "name" attribute, the values will be sent to the server as a comma-delimited list.

Just make sure that you do a trim of each element when you do a split, as some browsers will put spaces after the commas.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.