0

Which expands and collapses on click. Now I want to toggle an image along with it, which is a css background element. I am not able to figure out how.

<script>
      $(document).ready(function(){
        $(".panel_head").click(function(){
        //$(this).slideToggle("slow");

        $(this).next('div').slideToggle('slow', function() {

        });

       var myEl = $(this);
        var bgimg = myEl.css('background');
        if( bgimg === #00b0f0 url('http://localhost:8383/Buzz_HomePage/images/white_down_arrow.png') 5% center no-repeat ) {
        myEl.css({ 'background' : #00b0f0 url('http://localhost:8383/Buzz_HomePage/images/white_down_arrow.png') 5% center no-repeat });
        } else {
        myEl.css({ 'background' : #00b0f0 url('http://localhost:8383/Buzz_HomePage/images/white_down_arrow.png') 5% center no-repeat });
    }

      });  
      });


   </script>

This is my css

.panel_head {
    border-radius: 0 !important;
    color: white;
    font-size: 14pt;
    background: #00b0f0 url('http://localhost:8383/Buzz_HomePage/images/white_down_arrow.png') 5% center no-repeat;
    text-align: center;
    cursor: pointer;
}

And this is my html

 <div class="panel  custom_panel">

    <div class="panel-heading panel_head" id="panel-head1">
        <span class="expand-collapse-text exp" >
            Categories
        </span> 
    </div>

    <div class="panel-body" id="panel-body1">

        <ul class="left_options list-unstyled">

            <li>   <input value="1" id="ca1" class="handleclick" type="checkbox" name="ca"><label for="ca1"><span></span>ALL(A-Z Listing)</label>    </li>               
            <li>    <input value="2" id="ca2" class="handleclick" type="checkbox" name="ca"><label for="ca2"><span></span>Health Check Up</label>    </li>                               
            <li>    <input value="3" id="ca3" class="handleclick" type="checkbox" name="ca"><label for="ca3"><span></span>Treatment</label>          </li>   
            <li>    <input value="16" id="ca16" class="handleclick" type="checkbox" name="ca"><label for="ca16"><span></span> Procedure </label>     </li>   

        </ul>
    </div>
</div>

I am getting errors in my script.

ASh
  • 34,632
  • 9
  • 60
  • 82
Abdul Qhayum
  • 95
  • 1
  • 1
  • 8
  • 2
    the error is that you have missed the quotation at if condition it should be like `bgimg === 'url of image'` – Amit singh Aug 19 '15 at 07:58
  • But i am targeting the "background" property of that element. Not the "background-Image" property. Coz I want the background color along with the image for that element. – Abdul Qhayum Aug 19 '15 at 08:01
  • first fix error and update your question with a fiddle – Amit singh Aug 19 '15 at 08:03
  • You have error in console, all `#00b0f0 url(...` must be as string (surrounded by `'` or `"`) in JS code. Fix that error first. Also inside `if` body you do same thing no matter if condition is true or false. – Justinas Aug 19 '15 at 08:04
  • So you don't need the if statement =) – Repo Aug 19 '15 at 08:07
  • Apart from the error. If you wanted to make condition with "background" property, try to output the content first; e.g. using alert, so you got the string needed for your condition – Teguh Syahmar Aug 19 '15 at 08:15

2 Answers2

0

use css instead of js:

$(document).ready(function(){
  $(".panel_head").click(function(){
    $(this).next('div').slideToggle('slow', function() {});
    $(this).toggleClass('active');
  });  
});
.panel_head {
    border-radius: 0 !important;
    color: white;
    font-size: 14pt;
    background: #00b0f0 url('http://localhost:8383/Buzz_HomePage/images/white_down_arrow.png') 5% center no-repeat;
    text-align: center;
    cursor: pointer;
}


.panel_head.active {
    background: red url('http://localhost:8383/Buzz_HomePage/images/white_down_arrow_1.png') 5% center no-repeat;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="panel  custom_panel">
    <div class="panel-heading panel_head" id="panel-head1">
        <span class="expand-collapse-text exp">
            Categories
        </span>
    </div>

    <div class="panel-body" id="panel-body1">
        <ul class="left_options list-unstyled">
            <li><input value="1" id="ca1" class="handleclick" type="checkbox" name="ca"><label for="ca1"><span></span>ALL(A-Z Listing)</label></li>
            <li><input value="2" id="ca2" class="handleclick" type="checkbox" name="ca"><label for="ca2"><span></span>Health Check Up</label></li>
            <li><input value="3" id="ca3" class="handleclick" type="checkbox" name="ca"><label for="ca3"><span></span>Treatment</label></li>
            <li><input value="16" id="ca16" class="handleclick" type="checkbox" name="ca"><label for="ca16"><span></span> Procedure </label></li>
        </ul>
    </div>
</div>
MoLow
  • 3,056
  • 2
  • 21
  • 41
0

hey dude you just not workaround properly with the quotes( "" or '' ) thats why

for example i have given

if( bgimg === "#00b0f0 url('http://localhost:8383/Buzz_HomePage/images/white_down_arrow.png') 5% center no-repeat" ) {
  //your code
  }

here is the updated jsfiddle

Updated fiddle

you can prefer below link

How do I check if string contains substring?

Community
  • 1
  • 1
Himesh Aadeshara
  • 2,114
  • 16
  • 26