I have a button that is cloning an element on the page each time the user clicks it. It is duplicating a select list for them to choose another option.
However, its cloning the whole element including the option that was selected so all of the new ones appended have a default value which I dont want.
globalLocales = $("<div />").append($('[name=localeID]:first').clone()).html();
$('select').select2();
Is there a way I can remove the selected option during the cloning process so it doesn't carry over to the new element?
I tried using .removeProp('selected') in the append as well as .prop('selected',false); but that didn't work for me
One way to fix the proble is to select a nonexistent value:
$("<div />").append($('[name=localeID]:first').clone().val(-1)).html();
Or you can find selected option and remove selected attribute:
$("<div />").append($('[name=localeID]:first').clone()
.find(':selected').removeAttr('selected').end()).html();
but this is a little clumsy.
you can remove the selected attribute with this code.
$('[name=localeID] option:selected').removeAttr('selected');
Related
I am experimenting with select2 by adding custom tags to it.
I do this with this code:
$(".js-example-basic-multiple")
.on("select2:selecting",
function() {
e.params.args.data.text = parentTarget + e.params.args.data.text;
});
I am using the multiple attribute BUT i am using the optgroup elemnt of the select list and it acts as a parentId container where only one element per container can be selected. This is solved but what i can not do is remove "MycustomTag" from previous selected option.
MycustomTag is either
Users
Or
Tank
And when selecting a new option i want to remove the Tank/Users tag on the previous selected option. In my debuging process i found that this is getting overriden in the change event but I can't figure out how to stopp it.
the code is pretty huge so I created a fiddle
How can I remove the tag that I am setting?
After some extensive detective work I found a very simple answer...
$(this).data().data.text = $(this).data().data.text.replace(parentTarget, "");
fiddle has been updated
Need to clone select box from previous one ( ie, add select box 'n' number of time from the previous one )
But when I added each time, all the previously selected options should not be available in the cloned select box list.
$('.field_select_box_list').each(function(){
$(this).find('option:selected').remove();
});
This code removes the parent select boxes selected option too.. but I want remain them to have the selected option.
any help.
Your clone code can just do something like
$('el').clone().find('option:selected').remove().end()
The .end() causes the selector to return to being $('el') rather than the filtered option:selected selector, so you can continue running things like .appendTo() etc without needing to break the chain.
I think you need use one select_box original, hidden it. Then you can remove with select_box second not hidden or add more element from select_box original.
I have a Magento Website. I have two js files, one written in Prototype an one in jQuery.
I have two select elements, with about 10 options. From jQuery I'm changing the first select's attribute "selected" (an some more). In Prototype I have a class that creates dependencies between two select elements.
If I click on the first select element and choose an option with the mouse, on the second select element will appear only the options that are linked to the option I choosed in the first select element. This is allready done in prototype.
The idea is that I want to do the click/trigger automatically from jQuery. The first select change it's selected option. But the second select element display all the options (and should display only the options linked to the first select element).
On the Prototype file I have some Event Observers, and I guess that these observers are not triggered from jQuery:
var select_el = $('select_' + id);
...
Event.observe(select_el, 'change', this.update.bindAsEventListener(this));
Event.observe(select_el, 'swatches:change', this.update.bindAsEventListener(this));
...
update: function(event) { ...
So I need somehow to trigger these events from jQuery, or to set an autocheck in Prototype.
Please tell me what should I do.
I've read on stackoverflow that events from Prototype can't be triggered from jQuery. There should be a way to do that. I just haven't found it.
I've tried jQuery functions like:
jQuery("option[value='" + checkedId +"']").attr("selected","selected").parent().focus();
jQuery("option[value='" + checkedId +"']").parent().trigger("onchange");
jQuery("option[value='" + checkedId +"']").parent().trigger('change');
Where the "parent()" is the first select element. The first select element changes as I want, but the second select element should automatically change after the first select is changed. And this never happens. It works only if I click with the mouse on the select and choose manually an option. I want to do this from jQuery, without being necessary to manually click on the desired option.
Thanks for reading this, and now just give me a fix for this problem.
Regards.
I have a select list where one option is selected by default when the page loads.
When a different option is selected, the html I view in Firebug does not change.
I have a button that makes copies of this element, and would like the copies to have the same selected value as this one, but they all have the default value selected.
How can I make the copies have the same selected values as the original?
When you clone the element you should be able to set the cloned select's value to that of the original. E.g.,
var $select = $('#my-select'),
$clone = $select.clone().val($select.val());
// I don't know where you really want the clone, just an example...
$clone.insertAfter($select);
I have a div that is being used as a dialog with jQuery's .dialog(). This div has a select box with options. The options the user has already selected are displayed on the main page. They can remove options from the main page and can open the dialog multiple times to add more options.
I populate the select box with all possible options on page load, but then when I open the dialog box I use jQuery's hide() to hide the options that the user has already selected and are displayed on the main page. This adds the CSS display:none; to the element in question, which IE ignores on <option> tags and displays anyway.
I can easily enough call remove() instead and remove it from the DOM. However, if the user selects some options, them removes them on the main page, then opens the dialog again to select more options, the options are no longer in alphabetical order, the options that were removed from the DOM and put back in it are now at the bottom since I used .append().
Is there any way to get IE to hide <option> tags? Or is there a better way to do this? Or is there a way to insert in alphabetical order simply?
If you need to remove it from the DOM, you could store the options in an array. One array (or object) for each option list. Then removing options from the list itself is reversable. You can always rebuild the select menu again from the array. Just populate the array once the dom-ready event fires.
Demo online: http://jsbin.com/avuru
$(function(){
// Define variables to be used throughout this code
var colors = [];
var list = $("select[name='colors']");
var btnRestore = $("button[name='restore']");
var btnRemove = $("button[name='remove']");
// Cycle through each option, adding its value and text to our collection
$("option", list).each(function(i, o){
colors.push({ 'key':$(this).val(),'val':$(this).text() });
});
// Remove any remaining options, and add collection back into dropdownlist
$(btnRestore).click(function(){
$("option", list).remove();
for (var i = 0; i < colors.length; i++) {
$("<option>").val(colors[i].key).text(colors[i].val).appendTo(list);
}
});
// Remove first option from list - used to test 'Restore' functionality
$(btnRemove).click(function(){
$("option:first", list).remove();
});
});
I would clone the list of options before modifying it and keep the original around. That way you can reinsert it clean by replacing the modified one with the orignal.