0

I read Javascript selecting form elements using name attribute and from that have gotten the following form select working:

field_I_am_after: $(':input[name="panes[delivery][address][delivery_zone]"]').val(),

BUT it delivers the "selected" class NUMBER - in this case "12" from a group of State id's in an Ubercart form as follows:

<select id="edit-panes-delivery-address-delivery-zone" name="panes[delivery][address][delivery_zone]" class="form-select required">
 . . .  MORE . . . 
    <option value="11">Armed Forces Pacific</option>
    <option value="12" selected="selected">California</option>
    <option value="13">Colorado</option>
     . . .  MORE . . . 
</select>

I am trying to get the NAME for "selected" to go into / be the "field_I_am_after", that is i.e. => $(' . . . option:selected').val(), or .text(),

I also reviewed Get selected text from a drop-down list (select box) using jQuery and tried most of what was suggested. I am using JQuery 1.8 due to issues with ckeditor complaining about newer versions and not working with them

All the variants I tried to append this to my working statement have not worked

Can anyone see what have I done wrong - or incomplete ??

Community
  • 1
  • 1
wilburunion
  • 41
  • 1
  • 7
  • Try `$(':input[name="panes[delivery][address][delivery_zone]"] option:selected').text()` – Rayon Oct 21 '15 at 20:22

1 Answers1

1

Try this:

field_i_am_after: $(':input[name="panes[delivery][address][delivery_zone]"] option:selected').text()

The first part of the selector finds the <select> element, then drills down to option:selected to find the option that the user has chosen, and then uses .text() to get its text content rather than the value.

$(':input[name="panes[delivery][address][delivery_zone]"]').change(function() {
  $("#output").text(
    $(':input[name="panes[delivery][address][delivery_zone]"] option:selected ').text());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
<select id="edit-panes-delivery-address-delivery-zone" name="panes[delivery][address][delivery_zone]" class="form-select required">
  <option value="11">Armed Forces Pacific</option>
  <option value="12" selected="selected">California</option>
  <option value="13">Colorado</option>
</select>
<br>Chosen: <span id="output"><span>
Barmar
  • 741,623
  • 53
  • 500
  • 612