Change a form select from multiple to flat dynamically - javascript

I am using a plugin in Wordpress (taxonomy-picker) that allow me to search by multiple taxonomies.
I want to change two of the items that are "multiple" to flat type but I have to do this dynamically.
So right now I have for example:
<select name="bebida[]" multiple=""><option value="bebida=tp-all">Todos</option><option value="bebida=batidos" class="parent">Batidos</option><option value="bebida=cervezas" class="parent">Cervezas</option><option value="bebida=cervezas-importacion" class="parent">Cervezas Importación</option><option value="bebida=cocktail" class="parent">Cocktail</option><option value="bebida=copas" class="parent">Copas</option><option value="bebida=copas-premium" class="parent">Copas Premium</option><option value="bebida=gin-tonic" class="parent">Gin Tonic</option><option value="bebida=licores" class="parent">Licores</option><option value="bebida=vinos" class="parent">Vinos</option><option value="bebida=zumos" class="parent">Zumos</option></select>
That is created dynamically and I want to delete the multiple="" attribute in order to make them flat only for some of the selects that only have a "name" but not a class.
Is posible to do this??
Also I am not sure of how can I add a different class only to the attributes I want.
So I want to know how can I add a class with the name I want and delete multiple attr to ONLY the selects I want dynamically.
So for example I want to say something like:
"If select name is "bebida[]" add class "bebida" and delete the attribute multiple" and also "If the select name is "ambiente[] add class "ambiente" (but no delete anything)."
I am trying something like this to delete the multiple selector but looks like is not working:
$("select[name='localizacion[]']").removeAttr('multiple');
Thank you so much.
Edited with more information

WordPress ships with jQuery, so (inside document.ready):
$('select').each(function() {
if ( $(this).attr('name') == "bebida[]" && $(this).attr('class') == "bebida" ) {
$(this).removeAttr('multiple');
}
});
Syntax probably isn't dead on, but you get the idea.

Related

How to select for multiple fields with Jquery?

I currently have a form that is displaying fields that I do not want it to. There are 3 input fields associated with one form and the same form is being rendered multiple times.
I have my template set up to check for when this was only one value. I need to change it so the template will check for all of the values present & then decide to render.
So for example,
I have,
Div_id_form-0-x,
Div_id_form-0-y,
Div_id_form-0-z,
Div_id_form-0-location__location_category__requires_coordinates
What selector can I use to grab all of these to use in my template?
I need to grab them for each form. Where it says form-0 I have several of these forms repeating.
Then in HTML I have something like this.
$(function () {
// Determine weather we are in a modal window.
var context = $("#{{ contextId }}-form");
console.log("this is the {{ contextId }}" )
// Show required coordinates if location category requires them.
context.find("#id_location__location_category__requires_coordinates").change(function () {
if (context.find("#id_location__location_category__requires_coordinates").val() == 'Y') {
context.find('#id_needs_coordinates').removeClass('hide');
} else {
context.find('#id_needs_coordinates').addClass('hide');
}
});
});
Right now this is only checking for one value to determine whether to hide. I need it to check all of these for this value. Which is why I'm looking for a Jquery selector that can do such.
If you want a selector for IDs, you can use:
$("#Div_id_form-0-x, #Div_id_form-0-y, #Div_id_form-0-z, #Div_id_form-0-location__location_category__requires_coordinates")
Please see: https://api.jquery.com/id-selector/
Update
In the future, please clarify the entire issue and provide a proper example.
You can use the Attribute selectors.
$("[id^='Div_id_form-']")
This will select all IDs like:
Div_id_form-0-x
Div_id_form-1-x
Div_id_form-2-x
You will then select all of those elements.
See More: https://api.jquery.com/category/selectors/attribute-selectors/

change name of dynamically added select option element

I am dynamically generating some dropdowns and then allowing them some of those to be removed on board dyanmically. so that time, i have encountered an error of selection option (dropdown) elements id mismatch. something like below is.
newly added dropdowns.
select name="CSSAtapsClient[client_time_window_arr][0]" id="client_time_window_0">/select>
select name="CSSAtapsClient[client_time_window_arr][1]" id="client_time_window_1">/select>
select name="CSSAtapsClient[client_time_window_arr][2]" id="client_time_window_2">/select>
select name="CSSAtapsClient[client_time_window_arr][3]" id="client_time_window_3">/select>
after i dynamically remove them via javascript. (lets say i am removing the second one) so then new ones will be displayed as followings,
select name="CSSAtapsClient[client_time_window_arr][0]" id="client_time_window_0">/select>
select name="CSSAtapsClient[client_time_window_arr][2]" id="client_time_window_2">/select>
select name="CSSAtapsClient[client_time_window_arr][3]" id="client_time_window_3">/select>
So now the issue i have is, the names of the dropdowns are like this, (0,2,3)
CSSAtapsClient[client_time_window_arr][0],
CSSAtapsClient[client_time_window_arr][2],
CSSAtapsClient[client_time_window_arr][3]
So this is causing an error for me and i need to reorder this name and make it be like this, (0,1,2)
CSSAtapsClient[client_time_window_arr][0]
CSSAtapsClient[client_time_window_arr][1]
CSSAtapsClient[client_time_window_arr][2]
how can i simply rename these dropdowns name attribute (from 0 to how much ever dropdows are exisiting) ? appreciate an early reply
EDIT 1
I tried this, but didnt work.
$('#tbl_dynamic_call_dates select').each(function(i){
$(this).attr('name',"CSSAtapsClient[client_time_window_arr][i]");
});
You can simply reset the values using .attr() method:
$('#tbl_dynamic_call_dates select').attr('name', function(i) {
return 'CSSAtapsClient[client_time_window_arr]['+ i +']';
});
I did this,
$('#tbl_dynamic_call_dates select').each(function(i){
$(this).attr('name',"CSSAtapsClient[client_time_window_arr][" + i + "]");
});

Removing an <option> from a <select> tag according to previously selected <option> tag? (POLL Example)for

I have 50 rows of data and i want users to give them points by 1 to 50. I put dropdown boxes near them with options 1/50. But all i want is when a user selects 15(for example) for a row, 15 will be deleted from all other select tags of other rows. I am not as good as you in JavaScript. How can i accomplish this?
Hi casablanca i couldnt make he script you sent work. I need it to work on just one select tag so i give select tag an ID and an ID for the form too. I edit the scripts getElementsByTagName with getElementsByTagID (select tag's ID) to effect only one select tag. But the function doesnt triggered?
This might not be a very good idea, because it is very difficult for the user to modify choices -- for example, if I want to give 15 to a different option, I need to change the old one to something else and then change the new one to 15. Also, once all points have been assigned, it's impossible to make any changes because all options are gone.
A better idea would be to let the user do whatever he/she wants and then validate the form in the end. You could do that like this:
function validate() {
var used = []; // This array stores already used options
var rows = document.getElementById('myForm').getElementsByTagName('select');
for (var i = 0; i < rows.length; i++) {
var points = rows[i].value;
if (used[points]) {
// This value was already used
alert('Please choose a different value.');
rows[i].focus();
return false;
} else {
// This value was not used before; mark it as used now
used[points] = true;
}
}
return true;
}
And call this function in the onsubmit handler of your form:
<form id="myForm" onsubmit="return validate();">
EDIT1: id -> class
give each option the class of the number it is
<option class="15">15</option>
<option class="16">16</option>
etc.
Then jquery can remove() an item by class
$('.15').remove();
obviously have to do an on change and get the value just set. "remove()" is nice in this instance because I believe it will yank every instance of that class.
EDIT3: upon further consideration the above method would be further complicated by the need to not remove the "selected" option. Not going to figure out the exact method but I think changing the class from "15" to "selected15" with a $(this).append() or something of the sort before calling the remove would get the job done fairly safely.
EDIT2:
As noted by casblanca below this is not an ideal user interface at all for this type of input. You may want to look into this: http://www.utdallas.edu/~jrb048000/ListReorder/
Allows user to drag and drop items in a list to reorder them. Much more natural.

multiple set of check box?

im having a set of multiple check bxes, like one set contain 3 check box share same name another set contain 3 check sharing same name . how can i get the value of these two different set of check box using a single code .
var form = $('usersurvey');
checkboxes = form.getInputs('checkbox');
for(count=0; count< checkboxes.length; count++){
if(checkboxes[count].checked){
retValue=true;
break;
}
}
return retValue;
}
im tried with this code but it fetch all the check boxes , i want only that have the same name, i used prototype.js
if you give each set of checkboxes a different class you could select them using jquery like:
$(".className").each(function(index, element) { ... });
for instance. somebody may be able to improve on this solution by selecting by name (i wasnt sure if you could do that, i always just select by class).
EDIT: sorry i should elaborate probably. the $(".className") piece will select all the checkboxes of class 'className'. since it sounds like you want to DO something with each of them though, i just added the each call on the end. inside the each call, you can define a function (shown) that will do something for each checkbox that was selected. reference the jquery each docs here:
http://api.jquery.com/each/

How to create initially hidden field in the form layout?

Is it possible to create field in form layout that's initially hidden (completely, including field label)?
I don't want to call cmp.getEl().up('.x-form-item').setDisplayed[1] for these fields after rendering because it causes flicker and other “effects”.
[1] as I know that's the only way to hide form field including label.
You can setup the form field's xtype to hidden. So, you'll have something like this:
{
id:'my_field_id',
name: 'my_field_name',
xtype: 'hidden'
}
You can add the field like this:
Ext.getCmp("myFormPanel").add({
id:'my_field_id',
name: 'my_field_name',
xtype: 'textfield'
});
Ext.getCmp("myFormPanel").doLayout();
And remove it like this:
Ext.getCmp("myFormPanel").remove(Ext.getCmp("my_field_id"));
Ext.getCmp("myFormPanel").doLayout();
I hope this is what you want.
Sure, create a CSS class in your document with either (depending on your needs) 'display:none' OR 'visibility:hidden'. Call this class, say, 'invisible-class'.
Create another class with either 'display:block', 'display:inline' OR 'visibility:visible' and call this class say, 'visible-class'.
For the form field you wish to be invisible when its rendered you would set its 'cls' property to 'invisible-class', i.e.:
cls:'invisible-class'
And when you want the field to be visible, use the method:
addCls('visible-class')
ALTERNATIVELY- when you want to make the item visible use the method:
removeCls('invisible-class')
i.e. mycomponent.removeClass('invisible-class')
Hope this helps!
A simpler method if you just need to hide this single field and its field label would be to create a separate label object for it. It might be easier to just hide the label instead of deal with the CSS to hide the fieldLabel.
You might be able to add a listener to the render event of the layout managing panel to do something like this...
var cmp = Ext.getCmp(fieldID);
if((cmp.hidden) && cmp.previousSibling() != null)
&& (cmp.previousSibling().xtype =='label'){
cmp.previousSibling().hide();
}

Categories