0

I want to get a value stored in a variable Get_Current_id in a jquery clicked function and use in a javascript PrintContent(). Any help would be appreciated.

jQuery:

$('#AgeGroupWiseButton').click(function()
{
    var Get_Current_id = $('#AgeGroupWiseButton').val();
});

js:

<script>
function PrintContent()
{

    alert(Get_Current_id );

}
</script>

2 Answers 2

1

Remove the var. Otherwise, it's scoped to the current function and inaccessible outside of it.

Ideally, you should have these two functions in a closure of their own, but global variables will work too.

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

3 Comments

and if have like another jquery function and i first clicked jquery func 1 then clicked jquery function 2 then in that case which jqery func value will be store? thaanks for the andswer will accept in 9 mins.
Whichever one is called last.
okay. Acutally i don't know why but i am getting the same. will check my code. thanks
1

Declare Get_Current_id outside of the jquery event handler makes it accessible to other functions. Note that Niet the Dark Absol's suggestion to just remove the var from inside the jquery event handler is bad practice as it creates an implied global (which can create confusing problems).

var Get_Current_id;

$('#AgeGroupWiseButton').click(function()
{
    Get_Current_id = $(this).val(); // no need to re-select
});

function PrintContent()
{
    alert(Get_Current_id );
}

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.