Question

this is my html code: http://jsfiddle.net/Udxyb/417/

in the above code i want to add an image('for example plus or minus image') when toggle effect is taking effect??

<table id="main_table">
    <thead>
        <tr class="firstline">
            <th>Column1</th>
            <th>Column2</th>
            <th>Column3</th>
            <th>Column4</th>
        </tr>
    </thead>
    <tbody>
        <tr style="width:1002px; background-color:green; color:white">
            <td  colspan="" class="flip"> Section 1 </td>
             <td  colspan="" class="flip"> Section 1 </td>
             <td  colspan="" class="flip"> Section 1 </td>
             <td  colspan="" class="flip"> Section 1 </td>
        </tr>
    </tbody>
    <tbody class="section">
        <tr>
            <td>item 111</td>
            <td>item 112</td>
            <td>item 113</td>
            <td>item 114</td>





        </tr>
        <tr>
            <td>item 121</td>
            <td>item 122</td>
            <td>item 123</td>
            <td>item 124</td>
        </tr>
        <tr>
            <td>item 131</td>
            <td>item 132</td>
            <td>item 133</td>
            <td>item 134</td>
        </tr>
    </tbody>
    <tbody>
        <tr style="background-color:green; color:white">
            <td  colspan="" class="flip"> Section 2 </td>
             <td  colspan="" class="flip"> Section 2 </td>
             <td  colspan="" class="flip"> Section 2 </td>             <td  colspan="" class="flip"> Section 2 </td>
        </tr>
    </tbody>
    <tbody class="section">
        <tr>
            <td>item 211</td>
            <td>item 212</td>
            <td>item 213</td>
            <td>item 214</td>
        </tr>
        <tr>
            <td>item 221</td>
            <td>item 222</td>
            <td>item 223</td>
            <td>item 224</td>
        </tr>
        <tr>
            <td>item 231</td>
            <td>item 232</td>
            <td>item 233</td>
            <td>item 234</td>
        </tr>
    </tbody>
    <tbody>
        <tr style="background-color:green; color:white">
             <td  colspan="" class="flip"> Section 3 </td>
             <td  colspan="" class="flip"> Section 3 </td>
             <td  colspan="" class="flip"> Section 3 </td>
             <td  colspan="" class="flip"> Section 3 </td>
        </tr>
    </tbody>
    <tbody class="section">
        <tr>
            <td>item 311</td>
            <td>item 312</td>
            <td>item 313</td>
            <td>item 314</td>
        </tr>
        <tr>
            <td>item 321</td>
            <td>item 322</td>
            <td>item 323</td>
            <td>item 324</td>
        </tr>
        <tr>
            <td>item 331</td>
            <td>item 332</td>
            <td>item 333</td>
            <td>item 334</td>
        </tr>
    </tbody>
</table>

jQuery:

<script type="text/javascript">
jQuery(function($) {


    $("#polls").on("click", ".flip", function() {

        $(this)

            .closest('tbody')
            .next('.section')
            .toggle('fast');
    });
});
</script> 

in this jquery i have done coding to take toggle effect along with this i want an image to change???

Was it helpful?

Solution

Try this.

$('.flip').click(function() {
    var $$ = $(this);
      $$.closest('tbody')
        .next('.section')
        .toggle('fast');
    if(!$$.hasClass('active')){
        $$.parent().find('span.plus').hide()
        .end().find('.minus').show();
        $$.addClass('active');
    } else {
        $$.parent().find('.plus').show()
        .end().find('.minus').hide();
        $$.removeClass('active');
    }
});

Fiddle Demo

OTHER TIPS

You can use toggleClass() with jQuery to change the background image of the element. For example, if you want to change the background color of clicked <tr>.

$('.flip').click(function() {
    $(this).parent().toggleClass('opened');
});

Fiddle: http://jsfiddle.net/Udxyb/450/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top