html - Clever "add tag" javascript help needed -


I have this function to add options to a selection list:

  function addOption (Selectbox, value, text, cl) {var optn = document.createElement ("Options"); Optn.text = text; Optn.value = Value; If (cl == 1) {optn.className = "nav_option_main"; } Selectbox.options.add (optn); }  

I have just searched the "" tag, but I do not know how to implement it in the drop list.

In order to fill the sub-selection-box with the desired options, the above work is called on another selection box "Onc" event.

I do not think it should be very difficult

thanks and tell me if you need more input ...

Update:

When your function is triggered, Benjamin is copied to the opt-group label under one another. Ex:

  label label label optgroup.1 optgroup.2 optgroup.3 etc ...  

Thanks for the function though ... but how to do Can I fix this?

Here's an example:

And here Function - which is optimized with your original one:

  function addOption (selectbox, content, cl) {var addNode = selectbox; // cache the selection box, if we are only adding a single, groupless, option if (contents.length> 1) {// if we are adding a group addNode = document.createElement ("OPTGROUP") ; // Add node to a new optup group AddNode.label = Content [0]; // Use the first label (a string) as the group's label contents.shift (); // Remove the label from the array (hence it can not be used again)} (var i = 0; i & lt; content length; i ++) {var optn = document.createElement ("OPTION" ); Optn.text = content [ii] [0]; Optn.value = Content [i] [1]; If (cl == 1) {optn.className = "nav_option_main"; } AddNode.appendChild (optn); // Add this option to the selectbox or group. } If (content.length & gt; 1) {// If we have added a group, then we need to add the group to the selection box. Selectbox.appendChild (addNode); }}  

This version accepts one parameter 'content' - instead of a separate parameter for 'text' and 'value' - it accepts an array of one or more options is. Therefore, to add a single option, not as part of a group, do the following:

  var singleOption = [[Single option ', 1]]; AddOption (document.getElementById ('Selector'), Single Option, 0);  

The item in the content array is an array of only two objects - text, and value.

If you have a pile array in more than one of these materials, you create an opt-in group:

  var option group 1 = ['Group Title', ' Group 'on', 1], ['groupon.do', 2], ['group on three', 3], ['groupon. Four ', 4], [' group onif '', 5]]; AddOption (document.getElementById ('Selector'), optionGroup1, 0);  

The first item title for the group is, after that, it is repeating the same content as an item. When the function is trying to add more than one at a time, it is grouped together.

What are you doing after this?


Comments