2

I apologize if this has been answered here before, but I've searched and I'm not finding what I need. I'm creating a select box and populating the options with the name of a congressional bill and the option id with the bill number. Once the bill is selected, I need to fire off an even that will pass the id to a function. I can't figure out how to do that last part. I can add .click to the select element, but that takes the id of the first option, not the option that was picked. I can't get .click to work on the option itself. I've also tried using .focus and .select on the option element, but they aren't working either. How can I get jquery to recognize the option I've chosen? Any suggestions?

$.each(response.objects, function(){
            billNum = response.objects[count].id;
            $('#billSelector').append('<option id=\''+billNum + '\'>'     +response.objects[count].title + '</option>');
            count++;
        });//each
        $('option').focus(function(){
            alert($('option').attr('id'));
        });//function

2 Answers 2

6

You can listen for change event on select

$('#billSelector').on('change',function(){
  // get id of selected option
  var id = $('option:selected',this).prop('id');
});
Sign up to request clarification or add additional context in comments.

Comments

0

I think you're looking for something like this:

$("#billSelector").change(function(){
   var selectedOptionId = $("#billSelector option:selected").attr("id");
   alert("you have selected option: " + selectedOptionId);
});

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.