1

$(document).ready(function () {
    function somefunction(input) {
        $('div').html(input);
    }

    $("#assign").click(function () {
        //assign the value only
        somefunction("from assign to putput")
    });

    $("#output").click(function () {
        //run the function
        somefunction();
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input type="button" id="assign" value="assign" />
<input type="button" id="output" value="output" />
<div></div>

How to assign a value on a function in one button and run that function on another button?

Any suggestion is accepted.

3 Answers 3

3

Just assign the value to a variable outside the function scope.

$(document).ready(function () {
    var assignedvalue = "";
    function somefunction() {
        $('div').html(assignedvalue);
    }

    $("#assign").click(function () {
        //assign the value only
        assignedvalue = "from assign to putput";
    });

    $("#output").click(function () {
        //run the function
        somefunction();
    });
});
Sign up to request clarification or add additional context in comments.

2 Comments

I am not sure about using global variables but i always read that it is not a good practice to use global variables.
You should not use them if not needed. But this is typically a situation where you should use them, and it is the most convenient solution.
2

Why not to use global variable instead of assigning values in function?

$(document).ready(function () {
   var input = "";
    function somefunction() {
        $('div').html(input);
    }

    $("#assign").click(function () {
        //assign the value only
       input = "from assign to putput";
    });

    $("#output").click(function () {
        //run the function
        somefunction();
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input type="button" id="assign" value="assign" />
<input type="button" id="output" value="output" />
<div></div>

If you don't want to use global variables you can assign value to div attribute first, something like this:

$(document).ready(function () {
    function somefunction() {
        $('div').html($('div').attr('temp-value'));
    }

    $("#assign").click(function () {
        //assign the value only
       $('div').attr("temp-value","from assign to putput");
    });

    $("#output").click(function () {
        //run the function
        somefunction();
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input type="button" id="assign" value="assign" />
<input type="button" id="output" value="output" />
<div></div>

1 Comment

I am not sure about using global variables but i always read that it is not a good practice to use global variables
0

$(document).ready(function () {
var input="";//Global Variable initially assigned as empty
    function somefunction(input) {
        $('div').html(input);
    }

    $("#assign").click(function () {
        //assign the value only
        input="Some Value";//Assigning the global variable
    });

    $("#output").click(function () {
        //run the function
        somefunction(input);
        input="";//Clear the global variable after the success of 'somefunction'
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<input type="button" id="assign" value="assign" />
<input type="button" id="output" value="output" />
<div></div>

1 Comment

i can see two answers with this kind of solution. i don't know if you are just copying from them or you didn't see their answer.

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.