Jump to content

Creating or duplicating drop down menu in JS


director87

Recommended Posts

I have a dilemma. I have a form with a SELECT menu with several "treatments". I wan't users to be able to select as many treatments as they'd like. I've found an AJAX script that does most of the work for me, however, I'm having trouble creating the select menu in Javascript as a new element (and maintaining all the original options).

 

The script:

 

this.addElement = function( element ){

	// Make sure it's a file input element
	if( element.tagName == 'SELECT' ){

		// Element name -- what number am I?
		element.name = 'trat_' + this.id++;

		// Add reference to this object
		element.multi_selector = this;

		// What to do when a file is selected
		element.onchange = function(){

			// New file input
			var new_element = document.createElement( 'select' );

			// Add new element
			this.parentNode.insertBefore( new_element, this );

			// Apply 'update' to element
			this.multi_selector.addElement( new_element );
			// Update list
			this.multi_selector.addListRow( this );

			// Hide this: we can't use display:none because Safari doesn't like it
			this.style.position = 'absolute';
			this.style.left = '-1000px';

		};
		// If we've reached maximum number, disable input element
		if( this.max != -1 && this.count >= this.max ){
			element.disabled = true;
		};

		// File element counter
		this.count++;
		// Most recent element
		this.current_element = element;

	} else {
		// This can only be applied to file input elements!
		alert( 'Error: not a file input element' );
	};

};

 

 

My problem is : new_element = document.createElement( 'select' )

 

How do I go about creating the options in which the user selects? Or even better, preserve the original select box? Thanks for any and all help.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.