T0w3ntuM
T0w3ntuM

Reputation: 332

add class to nth-child with jquery

I'm trying to add a class using jquery to the nth-child so that it'll work in IE. It doesn't seem to be working, I have followed a few examples with no results. I have linked the fiddle

http://jsfiddle.net/aosto/XghbU/

<div id='tasklist'>
    <ul class='header'>
        <li>
            <div class='listitem head'>Number</div>
            <div class='listitem head'>Description</div>
            <div class='listitem head'>Start Date</div>
            <div class='listitem head'>Due Date</div>
            <div class='listitem head'>Edit/View</div>
            <div class='listitem head'>Complete</div>
        </li>
    </ul>
</div

 #tasklist ul {
clear:both;
list-style:none;
margin:0;
padding:0;
}
 #tasklist ul li {
clear:both;
margin:3px;
padding:3px;
}
 .listitem {
float:left;
display:inline-block;
}
 .listitem2 {
width:400px;
}

    $( document ).ready(function() {
        $('#tasklist ul li:nth-child(2)').addClass("listitem2");
    });


 <head>
    <link href='css/style.php' type='text/css' rel='stylesheet'>
<!--[if IE]>
<link rel="stylesheet" type="text/css" href="css/ie_style.css" />
<![endif]-->
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<script>
    $( document ).ready(function() {
        $('.listitem:nth-child(2)').addClass("listitem2");
    });
</script>
 </head>

Upvotes: 8

Views: 40316

Answers (2)

Nitin Kanish
Nitin Kanish

Reputation: 11

 jQuery(".menu-item").click(function(){
        jQuery(this).find('ul.sub-menu').toggleClass("display_both");
    });

Upvotes: 0

Fico
Fico

Reputation: 2407

Your selector should be the following in order to address your second div inside the list tag!!

$('#tasklist ul li div:nth-child(2)')

Or any case you should have at list two list elements in your markup if you are actually trying to target a list element.

Upvotes: 12

Related Questions