/// <summary>Removes the option at the given index.</summary> public void remove(int index) { // Get it at that index: HtmlOptionElement option = item(index); if (option != null) { // Remove it: option.remove(); } }
/// <summary>Sets the option at the given index as the selected one.</summary> /// <param name="index">The index of the option to select.</param> public void SetSelected(int index) { bool runOnChange = true; if (index == -3) { // Setup/ auto selected when the tag has just been parsed. runOnChange = false; index = 0; } // Get and set: HtmlOptionElement element = GetOption(index); SetSelected(index, element, runOnChange); }
/// <summary>Searches parent (which must be a parent) for the given node. /// Each time an option element is encountered, currentIndex is increased.</summary> private HtmlOptionElement GetOption(Node parent, ref int targetIndex) { if (parent.childNodes_ == null) { return(null); } // For each child node.. for (int i = 0; i < parent.childNodes_.length; i++) { // Get it: Node child = parent.childNodes_[i]; // Might be an optgroup containing it. Check if it is: if (child is HtmlOptionElement) { // Is it the one we're after? if (targetIndex <= 0) { // Yes! return(child as HtmlOptionElement); } // Just decrease index: targetIndex--; continue; } if (child.childNodes_ != null) { // Go recursive: HtmlOptionElement inChild = GetOption(child, ref targetIndex); if (inChild != null) { return(inChild); } } } // Not found here. return(null); }
/// <summary>Sets the option at the given index as the selected one.</summary> /// <param name="index">The index of the option to select.</param> /// <param name="element">The element at the given index.</param> /// <param name="runOnChange">True if the onchange event should run.</param> private void SetSelected(int index, HtmlOptionElement element, bool runOnChange) { if (element == SelectedNode_) { return; } Dom.Event e = new Dom.Event("change"); e.SetTrusted(false); // Cache previous: int prevIndex = SelectedIndex_; HtmlOptionElement prevNode = SelectedNode_; // Update current (so onchange gets the correct value): SelectedNode_ = element; SelectedIndex_ = index; if (runOnChange) { if (!dispatchEvent(e)) { // Restore to previous: SelectedIndex_ = prevIndex; SelectedNode_ = prevNode; return; } } // Update placeholder: if (SelectedNode_ == null) { // Clear the option text: Placeholder.innerHTML = ""; index = -1; } else { Placeholder.innerHTML = SelectedNode_.innerHTML; } }
/// <summary>Sets the given element as the selected option.</summary> /// <param name="element">The option to set as the selected value.</param> public void SetSelected(HtmlOptionElement element) { SetSelected(-2, element, true); }