Traverse/Locate and get Other Class
The problem is that I get too much output.
I would like to know the chain that would get me the other class name of a parent.
I have several forms with two words for the class. Starting with a selected <option>, I can get it's parent, the <optgroup>*. I ask for the name in the <optgroup> and use this as if it were the name of the <select multiple>. This is working fine.
But I am getting all the inputs from all the forms.
Maybe I am over-thinking this. But the solution I am looking for is this:
If I make selections and click the submit button in the green form, I need to end up with the string 'green' in a variable.
- HTML:
- =====
- <form class="frmclss green">
- <p class="actions">XX</p>
- <div class="foo">
- <input type="hidden" name="add" value="id" />
- <input type="submit" value="Submit" />
- <div>
- <span>
- <select multiple class="bar">
- <option value="">Select</option>
- <optgroup label="group1" name="bang[opt_id]" id="bang_id">
- <option value="value_id">value_name</option>
- </optgroup>
- </select>
- </span>
- </div>
- </div>
- </form>
- <form class="frmclss brown">...</form>
- <form class="frmclss yello">...</form>
- <form class="frmclss sandy">...</form>
Using the Firebug console, I can verify I am getting what I need (so far), but too much of it.
- FIREBUG:
- ========
- var selects = new Array();
- $(".frmclss select").find("option:selected").each(function(){
- var optgroup = $(this).parent().attr("name");
- console.log('The optgroup is: %s', optgroup);
- selects[optgroup] = $(this).val();
- console.log('The value is: %s', $(this).val());
- });
- console.log(selects);
- JAVASCRIPT:
- ===========
- $('form.frmclss').submit(function(){
- //---------------------------------------
- // Find the optgroup of the selected option and give that as the select name.
- var selects = new Array();
- $(".frmclss select").find("option:selected").each(function(){
- var label = $(this).parent().attr("name");
- selects[label] = $(this).val();
- });
- //---------------------------------------
- var add = $(this).serialize(); // Need to append the selects array
- var action = $(this).attr('action');
- var target = $('div#target');
- // added to accommodate no existing query string
- var parts = action.split("?");
- if(parts.length > 1) {action += "&";} else {action += "?";}
- $.ajax({
- url: action + 'godo=ajaxadd',
- type: 'POST',
- cache: false,
- data: add,
- complete: function(returned) {
- target.replaceWith(returned.responseText);
- }
- });
- return false;
- });
* Later, I will work on disabling all options within an optgroup once a selection has been made from that optgroup.