-1

I have searched around stack overflow and the web, and I just cant find an answer to this, at least which I don't understand. With my limited knowledge about this subject (jquery and java). So basically I want to change one of the css class (#navbar a) after some function happens.

I tried to implement this in the script, which didn't work:

$('link[href="Style.css"]').addClass('#navbar a').css('width', '20px');

This is the javascript / jQuery:

<script>
window.onscroll = function() {myFunction()};

var navbar = document.getElementById("navbar");
var sticky = navbar.offsetTop;

function myFunction() {
  if (window.pageYOffset >= sticky) {
    navbar.classList.add("sticky")
        navbar.style.align = 'auto';
        navbar.style.width = '100%';
        $('link[href="Style.css"]').addClass('#navbar a').css('width', '20px');
  } else {
    navbar.classList.remove("sticky");
      navbar.style.width = '80%';
  }
}
</script>

This is the css file / style file:

#navbar a {
  float: left;
  display: block;
  color: #f2f2f2;
  text-align: center;
  width: 25%;
  text-decoration: none;
  font-size: 35px;
  padding: 20px 0px;
}

This is the whole html code:

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width;initial-scale=1;height=device">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="Style.css">
</head>
<body style="background:#101010">
<div class="header">
  <h2>FELLE</h2>
</div>
<div id="navbar">
  <a class="active" href="">Hjem</a>
  <a href="Historie.html">Historie</a>
  <a href="Hyttefelt.html">Hyttefelt</a>
  <a href="Annet.html">Annet</a>
</div>
<div class="container" align="middle">
    <img src="Bilder/Felle_Butikk.jpg" alt="Felle butikk"/>
    <object data="Tekster/Felle_Butikk.txt"></object>
    <img src="Bilder/Solhomfjell.jpg" alt="Solhomfjell i nissedal"/>
    <object data="Tekster/Solhomfjell.txt"></object>
</div>

<script>
window.onscroll = function() {myFunction()};

var navbar = document.getElementById("navbar");
var sticky = navbar.offsetTop;

function myFunction() {
  if (window.pageYOffset >= sticky) {
    navbar.classList.add("sticky")
        navbar.style.align = 'auto';
        navbar.style.width = '100%';
        $('link[href="Style.css"]').addClass('#navbar a').css('width', '20px');
  } else {
    navbar.classList.remove("sticky");
      navbar.style.width = '80%';
  }
}
</script>
</body>
</html>
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

1 Answers1

1

$('link[href="Style.css"]').addClass('#navbar a').css('width', '20px');
<link href="Style.css" rel="stylesheet">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

You have the above code, but this doesn't modify the stylesheet. It modifies the link element.

DOM screenshot

Since the element isn't visible in the first place, giving it a width has no effect.


You could modify the style attributes of the actual elements (and include the stylesheet entirely):

jQuery('#navbar a').css('width', '20px');

Or you could see this question about changing the CSS rules themselves.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335