Click here to Skip to main content
15,886,724 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a table like below.

XML
<table>
<tr>
    <td class="cls dynamic_cls6">
        6
    </td>
    <td class="cls dynamic_cls3">
        Dinahar/EX9090/32
    </td>
    <td class="cls dynamic_cls4">
        EX9090
    </td>
    <td class="cls dynamic_cls4">
        Dinahar M A
    </td>
    <td class="cls dynamic_cls8">
        Self
    </td>
</tr>

<tr>
    <td class="cls dynamic_cls2">
        6
    </td>
    <td class="cls dynamic_cls1">
        Dinahar/EX9090/32
    </td>
    <td class="cls dynamic_cls4">
        EX9090
    </td>
    <td class="cls dynamic_cls7">
        Dinahar M A
    </td>
    <td class="cls dynamic_cls7">
        Self
    </td>
</tr>
</table>



And I want to read the second class using first class name in "mouseover" using jquery

C#
$(".cls").mouseover(function (evt) {
    alert(evt.currentTarget. 2ndClassname);
    return false;
});



The alert box should display like "dynamic_cls6,dynamic_cls4,etc..."
Posted

HTML class attribute can contain multiple CSS rule names separated by whitespace...
You can use split[^] method to create an array of all names and pick the one you interested in...
JavaScript
var classList = $(evt.currentTarget).attr('class').split(/\s+/);
alert(classList[1]);
 
Share this answer
 
v2
Comments
[no name] 11-Nov-14 6:44am    
Error shown "Uncaught TypeError: undefined is not a function "
Kornfeld Eliyahu Peter 11-Nov-14 7:01am    
forgot to wrap currentTarget - please see updated answer...
Try this code to do this task,

JavaScript
// handle the event
$('.cls').mouseover(function () {
   // alert the class (without cls in it)
   alert($(this).attr('class').replace('cls', ''));
});


Above code would take the entire class attribute of the element, will trim the cls section from the value returned and alert would show a dialog box having the second class name.

Test this in the fiddle[^] I've created.
 
Share this answer
 
v2
Comments
[no name] 11-Nov-14 5:56am    
The function doesn't trigger when mouse overon that class.
Afzaal Ahmad Zeeshan 11-Nov-14 5:58am    
Have you tried that fiddle? In that fiddle it works. Are you sure your HTML has that class cls in it. It will select only elements with the class cls.
[no name] 13-Nov-14 23:26pm    
Yes,

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900