Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get some parent element ID in order to send to javascript function

I have this table

<table id="tblId">
        <thead>
            <tr>
                <th>View Name</th>
                <th>View Description</th>                       
            </tr>
        </thead>
            <tbody>
                <tr id="1">
                    <td>Name</td>                    
                    <td><span onclick="jsFunction()">description</span></td>
                </tr>
                <tr id="2">
                    <td>Name</td>                    
                    <td><span onclick="jsFunction()">description</span></td>
                </tr>
            </tbody>
</table>

In the onclick event of each span i need to send the js function the row id of that specific row, How do i do that?

Thank you!

like image 982
Ovi Avatar asked Jan 01 '12 08:01

Ovi


People also ask

How do I find the ID of a clicked element?

To get the clicked element, use target property on the event object. Use the id property on the event. target object to get an ID of the clicked element.

What is $() in JavaScript?

$() The $() function is shorthand for the getElementByID method, which, as noted above, returns the ID of a specific element of an HTML DOM. It's frequently used for manipulating elements in a document. $() allows for shorter and more efficient JavaScript coding. Traditional method: document.

What is the difference between parentNode and parentElement?

Parent Element returns null if the parent is not an element node, that is the main difference between parentElement and parentNode. In many cases one can use anyone of them, in most cases, they are the same.


1 Answers

One way is you can send "sender" data with jsFunction as below,

... onclick="jsFunction(this)" ....

In your jsFunction you can find tr element,

    function jsFunction(sender){
        var tr = sender.parentNode.parentNode;
        alert( tr.getAttribute('id') );
    }
like image 53
tcak Avatar answered Oct 19 '22 20:10

tcak