Solved

How do I delete an option from html select element?

Posted on 2014-01-07
3
338 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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Html date format 9 20
Table doesn't show the lines! 3 24
Validating number not work with decimal 4 26
How do I update select listbox after search 2 25
Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
In this article you'll learn how to use Ajax calls within your CodeIgniter application. To explain this, I'll illustrate how to implement a simple contact form to allow visitors to send you an email through your web site.
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)

733 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