Nimitz E.
Nimitz E.

Reputation: 95

Add class not working on hyperlink

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...

Upvotes: 0

Views: 204

Answers (1)

Sachin
Sachin

Reputation: 40970

Wrap your script code inside the document.ready event.

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

Js Fiddle

Upvotes: 2

Related Questions