1

I am developing an ASP .Net MVC 3 application using C# and SQL Server 2005.

In a view, I have a DropDownList, a button.

How can I remove an item from the DropDownList for each click of the button.

I try to use javascript, but I think that's not working because when I click on the button, nothing happens.

This is the code :

<%:Html.Label("Poste :")%>
<%:Html.DropDownListFor(
    model => model.SelectedPoste,
    Model.PostesItems,
    new { @id = "poste" })%>

<div>
    <input type="submit"
           value="Enregistrer"
           id="btnSave"
           onclick="remove()" />
</div>

<script type="text/javascript">    
    function remove() {
        var rm = document.getElementById('btnSave');
        var poste = document.getElementById('poste');

        poste.removeItem(); 
    }
</script>
3
  • Can you confirm that the HTML generated from your Html.DropDownListFor actually has an ID of "poste" and not "SelectedPoste" ? Commented May 23, 2013 at 19:01
  • yes,,i tried that with other JS code but to disable a dropdownlist and it s worked Commented May 23, 2013 at 19:08
  • May I post an answer that uses jQuery library? It would make this much easier. Commented May 23, 2013 at 19:09

4 Answers 4

3

using jQuery

<select id="poste">
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
</select>

<br />

<input type="button" id="btnSave" value="Remove current item" />

<script type="text/javascript">
    $(function () {
        $('#btnSave').click(function () {
            $('#poste option:selected').remove();
            return false;
        });
    });
</script>

EDIT: binding the click event using jQuery

Sign up to request clarification or add additional context in comments.

16 Comments

@anouar Have you referenced jQuery script?
Well if you go with jquery, you might as well bind the even of the button here instead of mixing it with the html :) +1 though
@DimitarDimitrov there you go =)
@anouar Updated, there's a new return false; there now
@anouar You must implement this action inside that function(){...} over there. It won't do it magically =)
|
2
  1. The generated HTML will give the select element an ID of "SelectedPoste", not "poste" as you are attempting to set.

  2. Use remove to remove an item.

Change your javascript to be:

var poste = document.getElementById('SelectedPoste');

poste.remove(poste.selectedIndex); 

Edit: The generated HTML for the select will be:

<select id="poste" name="SelectedPoste">...</select>

Either of these two lines will get that elements:

var poste = document.getElementById('poste');

or

var poste = document.getElementById('SelectedPoste');

(Atleast in IE10)

Then to remove the selected item from the list, do:

poste.remove(poste.selectedIndex);

This does not remove the button :)

Edit 2: Like Dimitar's answer, you need to change your function name from remove to something else.

Once you do that, my code above works in IE and Chrome.

3 Comments

please look at my code, i don't have a select, how i will put the name ?
Your Html.DropDownListFor will generate the <select> tag that I mention above
Google Chrome will remove the button with my javascript code above. IE works as expected.
2

Using vanilla JavaScript you can do:

<script type="text/javascript">    
    function removeOption() {
       var posteElement = document.getElementById('poste');        
       var currentIndex = posteElement.selectedIndex;

       posteElement.removeChild(posteElement[currentIndex]);
       return false;
    }
</script>

That's all you need. Also make sure you rename your function to anything other than remove():

<input type="submit"
       value="Enregistrer"
       id="btnSave"
       onclick="removeOption()" />

Check out this (not very nice inline-fiddle).

However I'd strongly suggest at least looking into a library such as jquery, (this would be much easier than with vanilla.js). Check out Andre's answer.

6 Comments

poste is the entire select, not the option. OP wants to remove the currently selected option
@DimitarDimitrov Glad to help. +1 for the answer and for the kindness
Still the same problem :(((
@DimitarDimitrov sorry for the late,,,your solution is working but the page is refreshing after the click of the button
@anouar Ok firts of all remove you previous comments because thi thread is too long ... Second, what exactly disappeared when you're using jquery, how abou you make a fiddle with your issue and have me take a look at it
|
2

Try this:

$("#poste option[value='X']").each(function() {
    $(this).remove();
});

Or to be more terse, this will work just as well:

$("#poste option[value='X']").remove();

Example:

$("#btnSave").click(function(){
   $("#poste option[value='X']").remove();
});

Remember to use JQuery :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.