0

How to pass variable in javaScript click handlers.

I have assigned var condition_selction = ($(this).val()); in .change(function). How I can get the value of this variable in the second click handle which is $( "#process-signs-scrap-scrapped-button-enabled" ).click(function() {.

How I pass variable between these two functions.

$('input:checkbox').change(function() {
        /*get the parent node of the radio and then hide only its siblings*/
        $(this).parent('label').siblings().toggle();
        if ($('.sign-condition').is(':checked')) {
            var condition_selction = ($(this).val());
        }
        if ($('.sign-reason').is(':checked')) {
            var reason_selction = ($(this).val());
        }
        //Check that both select fields have a value
        if (condition_selction != null && reason_selction != null) {
            $("#process-signs-scrap-scrapped-button-disabled").hide();
            $("#process-signs-scrap-scrapped-button-enabled").show();
        } else {
            $("#process-signs-scrap-scrapped-button-disabled").show();
            $("#process-signs-scrap-scrapped-button-enabled").hide();
        }
    });

    $( "#process-signs-scrap-scrapped-button-enabled" ).click(function() {



            var process_signs_scrap_condition = condition_selction;


            var process_signs_scrap_reason = reason_selction


            // Open the timer modal
            $('#process-signs-scrap-scrapped-modal').modal('show');
1
  • 2
    What about global variable or scope of variable same for both of two function? Commented Sep 12, 2017 at 12:42

2 Answers 2

1

Declare both var globally, so you can access them in both functions

var condition_selction; // like this
var reason_selction;

$('input:checkbox').change(function() {
    /*get the parent node of the radio and then hide only its siblings*/
    $(this).parent('label').siblings().toggle();
    if ($('.sign-condition').is(':checked')) {
        condition_selction = ($(this).val()); // removed declaration 
    }
    if ($('.sign-reason').is(':checked')) {
        reason_selction = ($(this).val());
    }
    //Check that both select fields have a value
    if (condition_selction != null && reason_selction != null) {
        $("#process-signs-scrap-scrapped-button-disabled").hide();
        $("#process-signs-scrap-scrapped-button-enabled").show();
    } else {
        $("#process-signs-scrap-scrapped-button-disabled").show();
        $("#process-signs-scrap-scrapped-button-enabled").hide();
    }
});

$( "#process-signs-scrap-scrapped-button-enabled" ).click(function() {



        var process_signs_scrap_condition = condition_selction;


        var process_signs_scrap_reason = reason_selction


        // Open the timer modal
        $('#process-signs-scrap-scrapped-modal').modal('show');
Sign up to request clarification or add additional context in comments.

Comments

0

You can pass the value to a data attribute on the button and then on the onclick of the button access the data attribute and its good to go. I have modified some of your code to get a sample version going.

$('input:checkbox').change(function() {
        if ($('.sign-condition').is(':checked')) {
            var condition_selction = $(this).val();
            $("#process-signs-scrap-scrapped-button-enabled").attr('data-value', condition_selction );
        }
   })

    $( "#process-signs-scrap-scrapped-button-enabled" ).click(function() {
            var  condition_selction = $(this).attr('data-value');
           console.log("The value of the checkbox is "  + condition_selction);
            })
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label>
  <input type="checkbox" class="sign-condition" value="sign condition" /> click me first
</label>
<hr/>
<button type="button" data-value="" id="process-signs-scrap-scrapped-button-enabled">click me last</button>

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.