Using javascript variable Value form outside of method

This is the function I use to get the table row number

$('#myTable').find('tr').click( function(){
    var rownum=$(this).index()
  alert('You clicked row '+ (rownum) );
});

Variable rownumI want to use it for further processing. But I can’t get value beyond it. How to get it?

I tried using here .

<script>
find();
  alert('You clicked row '+ (window.rownum) );
</script>

But that did not work for me.

+4
source share
7 answers

There are several ways to do this.

What you will currently be working with a little modification:

var rownum;
$('#myTable').find('tr').click( function(){
  rownum=$(this).index()
  alert('You clicked row '+ (rownum) );
});

Hovever, the preferred method would be to pass the variable where it was needed:

function alertRowNumber(rowNumber) {
  alert('You clicked row '+ (rowNumber) );
}

$('#myTable').find('tr').click( function(){
  alertRowNumber($(this).index)
});

, .

+2
  <script langguage='text/javascript'>  
    var rownum = "";
    $('#myTable').find('tr').click( function(){
      rownum=$(this).index();
      alert('You clicked row '+ (rownum) );
    });
 </script>

var, rownum , , . find().

+2

rownum :

var rownum;
$('#myTable').find('tr').click( function(){
  rownum = $(this).index();
  alert('You clicked row '+ (rownum) );
});

rownum

$('#myTable').find('tr').click( function(){
  var rownum = $(this).index();      
  myOtherFunction(rownum);
});

function myOtherFunction(num)(){
  alert(num)
}
+1

rownum script

<script type=text/javascript>
var rownum;

function A(){
    rownum=5;
}
function B(){
    alert(rownum);//will throw 5
}

+1

.

SomeNameSpace={} // Creating name spacing object
   SameNameSpace.rownum='';
   $('#myTable').find('tr').click( function(){
      SameNameSpace.rownum=$(this).index()
      alert('You clicked row '+ (SameNameSpace.rownum) );
    });

, SameNameSpace.rownum

Using a global variable can cause problems later, because another developer can create a variable with the same name and in the global space, which will be a conflict

Contact here for more information on the namespace.

0
source

Make any function, and you probably get the value ...

 $('#myTable').find('tr').click( function(){
    rownum = $(this).index();
    //  alert('You clicked row '+ (rownum) );
    get(rownum);
 });

function get(rownum)
{
    alert(rownum);
}
0
source

Here's how I did it using the html5 session store

//function to set line index on clicking a table row - note there were several tables so i selected the first using .eq();  

$("table").eq(0).on('click','tr',function () 
    {
        var lineNum = $(this).closest("tr").index();
        sessionStorage.setItem('lineNumber',lineNum);
    });

//the following retrieves the line index from session storage and converts it to a number from a string (all session storage values are stored as strings)

var line_Number=parseFloat(sessionStorage.getItem('lineNumber'));
0
source

Source: https://habr.com/ru/post/1628603/


All Articles