Single box add options and remove options in form

Source: Internet
Author: User

Selection add option and place the last item

HTML code:

<form>
<select name= "Location" id= "Location" >
<option value= "Beijing1" >beijing</option>
<option value= "Shanghai1" >shanghai</option>
<option value= "CHENGDU1" >chengdu</option>
<option value= "CHANGSHA1" >changsha</option>
</select>
</form>

JS Code:

var select=document.forms[0].elements[' location ';

First method: Add a radio option
var newoption=document.createelement (' option ');
var newtext=document.createtextnode (' Guangzhou ');
Newoption.appendchild (NewText);
Newoption.setattribute (' value ', ' Guangzhou ');
Select.appendchild (newoption);

The second method: Also add the Radio option, is not a lot easier, haha but not compatible with IE8 and above version
var newsoption=new Option (' nanchang1 ', ' Nanchang ');
Select.appendchild (newsoption);

The third method: Add a radio box to see, the best solution, this is convenient and compatible
var nnewoption=new Option (' fengcheng1 ', ' Fengcheng ');
Select.add (nnewoption,undefined);

Removal options:

The first method: Select.remove (i);//index starting from 0

The second method: Select.options[i]=null;

Method Three: Select.removechild (Select.options[i])

Single box add options and remove options in form

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.