Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How do I delete an option from html select element?

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

Industry Leaders: 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

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
This article discusses how to implement server side field validation and display customized error messages to the client.
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

618 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