0

I an using ASP.NET MVC 4 to dynamically load a PartialView which contains a option. I want to bind a jQuery dblclick event to this and get the selected option, but the event is not firing. Here is my code:

 <select size="5" id="popup-list">
     <option value="test">test</option>
 </select>

jQuery code:

$(document).ready(function(){

    $("#popup-list").live("dblclick", function () {
        var name;
        $("select option:selected").each(function () {
            name = this;
            name = this.text();
        });
    });

});

I am not sure as the why this is causing a problem. The code is in a document.ready(), which should take care of the dynamic loading.

John 'Mark' Smith
  • 2,484
  • 7
  • 37
  • 65

2 Answers2

2

Use On method of jquery.

$("body").on("dblclick", "#popup-list", function () {
    var name;
    $("select option:selected").each(function () {
        name = this;
        name = this.text();
    });
});

Note: on method available on jquery version 7 onwards

Rashmin Javiya
  • 5,067
  • 3
  • 23
  • 46
2

Try one of this: Method:1

$(document).ready(function(){
    $("#popup-list").on("dblclick", function () {
        var name;
        $("select option:selected").each(function () {
            name = this;
            name = this.text();
        });
    });
});

Method:2 Or

$(document.body).on("dblclick", '#popup-list', function () {
---your code----
});

Method:3

$(document).on("dblclick", '#popup-list', function () {
---your code----
});
Umesh Sehta
  • 1,599
  • 9
  • 20