Solved

How do I delete an option from html select element?

Posted on 2014-01-07
3
340 Views
Last Modified: 2014-01-15
How do I delete an option from a html select element? I would like to do somehing like this:

var selectElement= document.getElementById(idparam);
selectElement.options[5].remove();

Open in new window


I am not able to do something like this because element exist in variable:

$('#idvalue option[value=' + val + ']').remove();

Open in new window

0
Comment
Question by:tolppa
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 250 total points
ID: 39762237
Check this : http://jsfiddle.net/jYcMA/1/

var idparam = "idvalue";

var selectElement= document.getElementById(idparam);
selectElement.options[5].remove(); // remove the 6th option : <option value="5">5</option>

var valueToRemove = 2;
$('#' + idparam + ' option[value=' + valueToRemove + ']').remove(); // remove option having value 2

Open in new window

0
 
LVL 9

Assisted Solution

by:Sar1973
Sar1973 earned 250 total points
ID: 39764821
In Javscript:
var x = document.getElementById("idparam");
x.remove(x.selectedIndex);
where the selected index is the number (0-N) of the option to be removed.
0
 
LVL 1

Author Closing Comment

by:tolppa
ID: 39782652
Thanks. Seems like selectElement.options[5].remove(); does not work with IE. x.remove(x.selectedIndex); seems to work better.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

724 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question