0

I have this table:

<table>
        <tr class="initial" onmouseover="this.className='highlight'" onmouseout="this.className='initial'">
            <td rowspan="2">ELEMENT 1</td>
            <td class="value">val 1</td>
        </tr>
        <tr class="initial" onmouseover="this.className='highlight'" onmouseout="this.className='initial'">
            <td>val2</td>
        </tr>
        <tr class="initial" onmouseover="this.className='highlight'" onmouseout="this.className='initial'">
            <td rowspan="2">ELEMENT 2</td>
            <td>val a</td>
        </tr>
        <tr class="initial" onmouseover="this.className='highlight'" onmouseout="this.className='initial'">
            <td>val b</td>
        </tr>         
    </table>​

Each couple of tr should be colored when mouse is over. This is not possible (e.g. if mouse is over ELEMENT 2 cell, then "Element 2" and "Value a" cells are colored, while "Value b" is not.). How can I group element together? What I want is: if I'm with mouse upon Element 1, Value 1 or Value 2 cell both (three) cells need to be colored. What can I do? Here is an example of my problem: http://jsfiddle.net/PA4y6/

4
  • Can you clarify what it is you are trying to achieve?
    – Whymarrh
    Commented Sep 16, 2012 at 18:19
  • can you use jQuery or some other libraries?
    – haynar
    Commented Sep 16, 2012 at 18:27
  • 1
    You should try to avoid using on*="h()" or elem.on* = h(){}; You could put the event listener on the tr elements, and in your function check the e which is the first argument and use it's e.target or e.srcElement to learn about the event itself, which is using a parent (tr? table?)j element to delegate it's event to another, lower element. jQuery is all over delegates, for instance. Commented Sep 16, 2012 at 18:35
  • Why should I prefear delegate approach rather than direct approach?
    – BAD_SEED
    Commented Sep 16, 2012 at 22:22

1 Answer 1

2

one way would be to wrap the rows you want to be grouped in a <tbody> and bind the listener to that.

see this fiddle

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.