-2

I want to change the class of a particular <li></li> element and I found a few ways to do so, but non of them worked, can you please tell me the reason why my code is not working >

<script type="text/javascript">
        $('.header a[href="register_friend.php"]').parent().setAttribute("class", "active");
    </script>

<header class="header" >
    <nav><ul>
            <li class="active"><a href="index.html">Home</a></li>
            <li><a href="register_friend.php">Register a friend</a></li>
            <li><a href="">Register Yourself</a></li>
            <li><a href="">Contact</a></li>
            <li><a href="index.html">Rate</a></li>
        </ul>
    </nav>
</header>

I would like to add that I am using an external header, if that brings any change.

<body >
<div id="header"></div>

</body>
Habib Ansari
  • 3
  • 1
  • 6

4 Answers4

1
<script type="text/javascript">
  document.addEventListener("DOMContentLoaded", function() {
    $('.header a[href="register_friend.php"]').parent().addClass("active");
  });
</script>

Look for addClass in API documentation https://api.jquery.com/addClass/

https://codepen.io/codingonHP/pen/OggeJb

wrt to addEventListner : https://developer.mozilla.org/en/docs/Web/Events/DOMContentLoaded and for IE: addEventListener not working in IE8

Vishal Anand
  • 1,431
  • 1
  • 15
  • 32
0

Try wrap your script in this way:

$(document).ready(function(){
     // Note:jquery use 'attr', instead of 'setAttribute' to set attribute.
    $('.header a[href="register_friend"]').parent().attr("class", "active");
})
miao.wang
  • 64
  • 1
  • 9
0

You either need to get the DOM node itself, so you can use the native setAttribute, or else use the jQuery .attr() on the jQuery object. Either of these will work (assuming the DOM nodes exist at the time the script is run -- do usual document.ready dance if necessary.)

// use the DOM node:
$('.header a[href="register_friend.php"]').parent()[0].setAttribute("class", "active");

// or use the jQuery object:
$('.header a[href="register_friend.php"]').parent().attr("class", "active");
.active {
  border: 1px solid #F00
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<header class="header">
  <nav>
    <ul>
      <li><a href="index.html">Home</a></li>
      <li><a href="register_friend.php">Register a friend</a></li>
      <li><a href="">Register Yourself</a></li>
      <li><a href="">Contact</a></li>
      <li><a href="index.html">Rate</a></li>
    </ul>
  </nav>
</header>

If you know there will only be one matching element, those methods are equivalent. If you need to be able to handle more than one matching element, use the jQuery .attr() method which will apply to all nodes within the set.

(...but with that said, .addClass() is probably a safer choice than setting the class directly with .attr() or .setAttribute(), because the latter two will overwrite any the other classnames presently on the element.)

Daniel Beck
  • 20,653
  • 5
  • 38
  • 53
0

First you should use addClass, not set the attribute.

Apart from that you should wrap the jQuery function into $(document).ready(function() { ... }) if you want this to happen on page load:

$(document).ready(function() {
        $('.header a[href="register_friend.php"]').parent().addClass("active");
});
.active {
border: 1px solid red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<header class="header" >
    <nav><ul>
            <li class="active"><a href="index.html">Home</a></li>
            <li><a href="register_friend.php">Register a friend</a></li>
            <li><a href="">Register Yourself</a></li>
            <li><a href="">Contact</a></li>
            <li><a href="index.html">Rate</a></li>
        </ul>
    </nav>
</header>
Johannes
  • 64,305
  • 18
  • 73
  • 130