william
william

Reputation: 606

send option selection via. jquery

I have some problems sending an id though jquery. I have a form select with some values, i want to "grap" the id when change option selection.

Output error: [object Object] !! :/

<select name="case_name" id="case_id">
            <option value="10009" >Case 1</option>
            <option value="10010" >Case 2</option>
            <option value="10011" >Case 3</option>
            </select>



    var PARAMS = { "case_id": 10009 }; 

$("#case_id").change(function() {  
    var CASE_ID = $(this).val();
    var PARAMS = { "case_id": CASE_ID };
    alert(CASE_ID);
});

Upvotes: 0

Views: 754

Answers (2)

Anurag
Anurag

Reputation: 141869

CASE_ID is defined inside an anonymous function and is only valid inside this function. And inside the callback you can simply refer to the select box as this.

$("#case_id").change(function() {
    var CASE_ID = $(this).val();
});

Outside the anonymous function, it does not exist.

var PARAMS = { "case_id": CASE_ID };

Either modify the change callback to include the PARAMS object.

$("#case_id").change(function() {  
    var CASE_ID = $(this).val();
    var PARAMS = { "case_id": CASE_ID };

});

Or fetch the value again

var PARAMS = { "case_id": $("#case_id").val() };

Or maybe define a function that you pass the new selected value to:

function sendValue(value) {
    var PARAMS = { "case_id": value };
    // send the value somewhere
}

$("#case_id").change(function() {
    var CASE_ID = $(this).val();
    sendValue(CASE_ID);
});

Upvotes: 3

jimyi
jimyi

Reputation: 31191

var CASE_ID = $("#case_id option:selected").val();

Upvotes: 3

Related Questions