Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem with each()

Tags:

jquery

<script type="text/javascript">
    //<!--
$(document).ready(function() {

 $('ul.course-nav li a').each(function() {
  alert(5);
  if ('#' == $(this).attr('href')) {
   $(this).addClass('lessOpacity');
  }
 });

});    //-->
</script>

HTML of course contains searched elements:

  <ul class="course-nav">
   <li><a href="navigator.php?kam=zakladnyNahladKurzu&amp;id=1&amp;pos=2" class="next"><img src="css/images/16_arrow_right.png" alt="next"></a></li>

   <li><a href="#" class="prev"><img src="css/images/16_arrow_left.png" alt="prev"></a></li>
   <li><a href="navigator.php?kam=zakladnyNahladKurzu&amp;id=1" class="start"><img src="css/images/16_arrow_first.png" alt="start"></a></li>
  </ul>

Yet it doesn't work. Not even alerts pop up. Any suggestions?

like image 479
Richard Knop Avatar asked Jun 25 '26 00:06

Richard Knop


2 Answers

You don't need each there, you can simply do:

$('ul.course-nav a[href="#"]').addClass('lessOpacity');

The jQuery's implicit iteration will look through all links whose href is set to # and add the class accordingly. Make sure to wrap your code in ready handler:

$(function(){
  $('ul.course-nav a[href="#"]').addClass('lessOpacity');
});
like image 56
Sarfraz Avatar answered Jun 27 '26 15:06

Sarfraz


intead of

  $('ul.course-nav a').each(function() {
    alert(5);
    if ('#' == $(this).attr('href')) {
    $(this).addClass('lessOpacity');
   }
 });

replace with

 $('ul.course-nav li a').each(function() {
    alert(5);
    if ('#' == $(this).attr('href')) {
    $(this).addClass('lessOpacity');
   }
 });
like image 30
Manie Avatar answered Jun 27 '26 13:06

Manie



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!