?
Solved

How do I delete an option from html select element?

Posted on 2014-01-07
3
Medium Priority
?
342 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 1000 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 1000 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

Interactive Way of Training for the AWS CSA Exam

An interactive way of learning that will help you visualize core concepts so that you can be more effective when taking your AWS certification exam.  Built for students by a student to help them understand the concepts that they are being taught.

Question has a verified solution.

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

There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
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 code hide show and toggles. 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…
Suggested Courses

764 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