Solved

How do I delete an option from html select element?

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
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…

743 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now