0

how can i pass a variable inside a event handler to another event handler

$("#vaccines").find("a").click(function() {
    var sendvaccineval = $(this).attr('id');
});

$("#options").find("a").click(function() {
    var sendbuttonval = $(this).attr('id');
});

I'm planning to use the attribute of the first event handler to the second one

I have two modals so theres a modal inside a modal, in the second modal i need to use the attribute of the first modal

eisbehr
  • 12,243
  • 7
  • 38
  • 63
Christian
  • 13
  • 1
  • 5
  • a global variable would be one obvious way to allow a variable to be accessed from different functions. – ADyson Sep 26 '16 at 13:55
  • 2
    Why would you be calling an event handler from another event handler? If there is common code, move it out into a separate function that takes arguments and that both handlers can call. –  Sep 26 '16 at 13:58
  • how? i can't take the var sendvaccineval = $(this).attr('id'); from the event handler it will not work – Christian Sep 26 '16 at 13:58
  • i dont get what you're saying, im a noobie, can you please give me a short code for that? – Christian Sep 26 '16 at 14:00
  • Difficult to give you code when your explanation is so unclear. But inside each event handler function, have a call to a third function that takes parameters. –  Sep 26 '16 at 14:02
  • Event handlers are not modals, cannot be modals, they can bring up a modal, but that is a different thing. Your entire question is very unclear, you need to give more detail on what you are trying to do. –  Sep 26 '16 at 14:03
  • but $("#vaccines").find("a").click and $("#options").find("a").click are different i can only use the parameter inside it – Christian Sep 26 '16 at 14:04
  • No, you can use the parameter in a third function by passing it to it. I'll post an answer with code. –  Sep 26 '16 at 14:05
  • @ADyson - people way too often jump to using globals, especially in javascript. Easy way to pass information between functions. But also easy way to create unpredictable side effects. Most of the time, global variables are unneccessary. Being a child of the early procedural programming days, when globals were often the only way to do something like this, I praise God we are well out of those days, and I am always **very** reluctant to suggest global variables. –  Sep 26 '16 at 14:22

2 Answers2

0

If you need to execute common code between two event handlers, pass the parameter to a third function that has the common code.

$("#vaccines").find("a").click(function() {
    var sendvaccineval = $(this).attr('id');
    doSomething(sendvaccineval);
});

$("#options").find("a").click(function() {
    var sendbuttonval = $(this).attr('id');
    doSomething(sendbuttonval);
});

function doSomething(myButton)
{
    // ....  do something with the button...
}

And avoid globals unless they are really necessary, which I seriously doubt they are in this case

Community
  • 1
  • 1
0

You can create a simple input hidden.

<input type="hidden" id="hide_value" value="">

I recommend to you, indicate and ID for your link

<a id="link_vaccines" href="#">
$('#link_vacciones').on("click",function(){
  var id = $(this).attr("id");
  $('#hide_value').val(id);
});

<a id="link_options" href="#">
$('#link_options').on("click",function(){
  var id = $(this).attr("id");
  $('#hide_value').val(id);
});

console.log($('#hide_value').val());

You can put the value in a global var, or in anything really. But I don't know what do you want achieve whit this . There's a lot of thing that you can make for this.

Netzach
  • 321
  • 2
  • 13