0

jQuery

if ($('.panel a').hasClass('collapsed')) {
    $('.panel a').addClass('no-print');
}   

HTML

<div class="panel panel-default">
    <a data-toggle="collapse" data-parent="#accordion" href="#collapse1" class="collapsed">
</div>

I have my jQuery and HTML above, I wanted to add the no-print, which triggers the no-print CSS style that won't print its content... However, it's not working. Is my jQuery wrong?

I have followed this instruction:

  1. Check to see if href has class, if so, grab the id
  2. Determine if an element has a CSS class with jQuery

I don't know what else to do...

Community
  • 1
  • 1
Nimitz E.
  • 95
  • 1
  • 13
  • 1
    Where is that `script` relevant to the rest of your HTML – tymeJV Sep 25 '14 at 19:22
  • You should wrap your jQuery code in the '$(document).ready();' event – Mark Sep 25 '14 at 19:24
  • Hello @MarkNijboer, I tried it, but it doesn't work. – Nimitz E. Sep 25 '14 at 21:03
  • @tymeJV that is the only script that I want to the no-print class, so when the user click the print button, then it wont display that area. Which when it dont have a class, it shows a content, and when it have the collapse class, it wont show the content. – Nimitz E. Sep 25 '14 at 21:05

1 Answers1

2

Wrap your script code inside the document.ready event.

$(document).ready(function() {
  if ($('.panel a').hasClass('collapsed')) {
    $('.panel a').addClass('no-print');
  }
})

Js Fiddle

Sachin
  • 40,216
  • 7
  • 90
  • 102
  • what problem are you facing ? I have also added a sample demo which help you understand the problem. – Sachin Sep 26 '14 at 19:46