Reputation: 331
I'm trying to do something somewhat basic but not sure how to do it given the JS function I have. I essentially want to "return" a js variable outside a js function so I can do stuff with it. The function in question is a function written to capture an input to a modal and that is why it is written the way it is.
Here is the function:
<script>
$('#upload-images-modal').on('show.bs.modal', function gfunction1(e, f, g) {
var yourParameter = e.relatedTarget.dataset.yourparameter;
document.getElementById("demo2").innerHTML = yourParameter;
});
</script>
It does pass the input to the modal and this function works with that, the output to "demo2" works as long as I do that statement within the function.
I would like to "pass" or "return" the variable outside of the function somehow.
Upvotes: 0
Views: 51
Reputation: 1
You could define the function outside of event pattern. Not certain which variable you want to return?
<script>
function gfunction1(e, f, g) {
var yourParameter = e.hasOwnProperty("relatedTarget")
? e.relatedTarget.dataset.yourparameter
: e.dataset.yourparameter;
document.getElementById("demo2").innerHTML = yourParameter;
// return variable
}
$("#upload-images-modal").on("show.bs.modal", gfunction1);
// call function outside of event
// pass `$("#upload-images-modal")[0]` as `e`
gfunction1($("#upload-images-modal")[0]);
</script>
Upvotes: 1