MD. ABDUL Halim
MD. ABDUL Halim

Reputation: 722

How to get dropdown changed value from in jquery to another function

This is my code

$(function(){
    $('#dropdown_name').change(function(){
        var V = $('#dropdown_name').val();
        var T = $('#dropdown_name :selected').text();
    });
});

I want to send V and T's value below function

function xyz()
{
    a = V;
    b = T;
    alert(a);
    alert(b);
} 

In Function xyz, I have to call many function so I don't want, it take as parameter of xyz function. So how can I get data in another way when I will select a dropdown then it will display alert(a and b) that value and text of dropdown.

How to solve it, please any suggestion.

Upvotes: 3

Views: 1875

Answers (5)

tymeJV
tymeJV

Reputation: 104775

Call xyz from the change event and assign the variables in xyz.

$("#dropdown_name").change(function() {
    xyz();
});

function xyz() {
    var a = $("#dropdown_name option:selected").val();
    alert(a);
}

Demo: http://jsfiddle.net/D48FY/

Upvotes: 1

Rafay
Rafay

Reputation: 31033

you can call your function instead of the anonymous callback on change like if you have fol markup

<select>
    <option value=""> select </option>
    <option value="2">SO</option>
    <option value="3">google</option>
</select>

js

function callback(e){
    console.log($(e.target).val());

}

$(function(){
 $("select").change(callback);
});

you can replace the callback function with xyz

DEMO

Upvotes: 1

Iori
Iori

Reputation: 660

you can use global values too

var V = ""; 
var T = "";
$(function(){
    $('#dropdown_name').change(function(){
        V = $('#dropdown_name').val();
        T = $('#dropdown_name :selected').text();
    });
});
I want to send V and T's value below function

function xyz()
{
    a = V;
    b = T;
    alert(a);
    alert(b);
} 

Upvotes: 1

jjnguy
jjnguy

Reputation: 138864

You can use global variables instead:

function xyz()
{
    a = window.V;
    b = window.T;
    alert(a);
    alert(b);
} 

You will call the function like so:

$(function(){
    $('#dropdown_name').change(function(){
    window.V = $('#dropdown_name').val();
    window.T = $('#dropdown_name :selected').text();
    });
});

Upvotes: 2

Adam Coulombe
Adam Coulombe

Reputation: 1505

if you dont want to pass as parameters you would likely need to define the variables in a scope available to all functions that will use the variables.

var V,T;
$(function(){
    $('#dropdown_name').change(function(){
    V = $('#dropdown_name').val();
    T = $('#dropdown_name :selected').text();
    xyz();
    });
});

function xyz(){
    alert(V); alert(T)
}

Upvotes: 1

Related Questions