Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript rowIndex method is not working

I am using the rowIndex property of TR but it is not working. Please let me know if i am doing something wrong here.

function myMethod(){
                alert ( this.parent.rowIndex  );   // parentNode is also used
            }

Html

<table border="1">
            <tr>
                <td onclick="myMethod();">1.1</td>
                <td>1.2</td>
                <td>1.3</td>
            </tr>
            <tr>
                <td onclick="myMethod();">2.1</td>
                <td>2.2</td>
                <td>2.3</td>
            </tr>
            <tr>
                <td onclick="myMethod();">3.1</td>
                <td>3.2</td>
                <td>3.3</td>
            </tr>
            <tr>
                <td onclick="myMethod();">4.1</td>
                <td>4.2</td>
                <td>4.3</td>
            </tr>
        </table>
like image 819
Rakesh Juyal Avatar asked Dec 18 '22 04:12

Rakesh Juyal


2 Answers

the "this" in this.parent.rowIndex is the window. Not the td element. Try

<td onclick="myMethod(this);">1.1</td>

function myMethod(obj){ alert ( obj.parentNode.rowIndex );} 
like image 137
Murali VP Avatar answered Jan 04 '23 11:01

Murali VP


How about like this?

<td onclick="myMethod(this);">1.1</td>

...

function myMethod(obj){
    alert ( obj.parentNode.rowIndex  );   // parentNode is also used
}
like image 30
YOU Avatar answered Jan 04 '23 10:01

YOU