4

This is the function which I am using to get Table row number

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

The variable rownum I want to use it for further processing. But I am not able to get the value outside it. How to get it ?

I have tried as suggested here.

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

But it did not worked out for me.

7 Answers 7

2

There are a few ways you can to this.

What you have currently would work 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's needed:

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

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

There may be an even better solution for your specific scenario, if you want to post an in-context snippet.

Sign up to request clarification or add additional context in comments.

Comments

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

Do not usevar, just define rownum outside so it will be global and you can access anywhere. Also you can not call find() directly.

Comments

1

You can set rownum global and access from outside of the function:

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

Or pass it rownum as parameter to other function

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

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

Comments

1

Declare rownum directly below script tag

<script type=text/javascript>
var rownum;

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

Comments

0

You can use namespacing technique.

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

So anywhere you can use this variable by simply calling SameNameSpace.rownum

Using global variable may create issues in later, as other developer may create a variable by same name & in global space that will be a conflict

Refer Here for more information on namespace

Comments

0

Make function any and you will get value for sure ...

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

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

Comments

0

This was how I did it using html5 session storage

//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'));

Comments

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.