1

Hi i am trying to create dropdownlists dynamically and populating the contents via an ajax call, this sort of works in the sense that the drop down is created and populated as required but its 'change' event does not fire. I am posting the code below, if anyone can spot something obvious, can you please let me know regards

$(document).ready(function () {

 $("#positions select").change(function (e) {
    alert("in");
    var id = $("#category_id").val();
    $.getJSON("/Category/GetSubCategories/" + id, function (data) {
    if (data.length > 0) {

        alert(data.length);
        var position = document.getElementById('positions');
        var tr = position.insertRow(7);
        var td1 = tr.insertCell(-1);
        var td = tr.insertCell(-1);
        var sel = document.createElement("select");
        sel.name = 'sel';
        sel.id = 'sel';
        sel.setAttribute('class', 'category');

        td.appendChild(sel);
        $.each(data, function (GetSubCatergories, category) {
        $('#sel').append($("<option></option>").
       attr("value", category.category_id).
      text(category.name));

        });
    }
    });
    });
 }); 
1
  • how to get Id and value of the current dropdown that is firing the event? Thanks for your help and also the speed of response Commented Apr 22, 2010 at 11:41

1 Answer 1

3

If your drop down is dynamically generated then you have to bind the change event using .live() handler. live() attaches a handler to the event for all elements which match the current selector, now or in the future.

$("positions select").live("change", function(){
});

Edit

To get the id

$(this).attr("id");

To get value

$(this).val();

To get class name

$(this).attr("class");

inside the change event handler.

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

1 Comment

Hello, How can I get the id and value and also class name of the element/dropdownlist currently firing this event? $("#positions select").live("change", function () { }

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.