Search code examples
jqueryhtmlfunctionglobal-variables

How to read the variable out of a click event function in jquery


i am generating a value to a varible on a click event.

Which i need to use globally or out of it function event.

$("#Coil_Data tr").on('click', function (){
          var selectedcoilrowid = $(this).closest('tr').find('#Coil_ID').text()
          $('#selected_coil_id_title').text('Fan for the Coil ID : '+selectedcoilrowid)
      })

Solution

  • Though variables should be avoided to be declared global, you can use the global object.

    $("#Coil_Data tr").on('click', function (){
              global.selectedcoilrowid = $(this).closest('tr').find('#Coil_ID').text()
              $('#selected_coil_id_title').text('Fan for the Coil ID : '+global.selectedcoilrowid)
          })