-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>
4
  • please add html code also. thanks Commented Mar 20, 2019 at 10:07
  • 2
    If you want to change the css of the ELEMENT then use $('#navbar a').css('width', '20px') Commented Mar 20, 2019 at 10:08
  • 1
    Possible duplicate of Change CSS class properties with jQuery Commented Mar 20, 2019 at 10:09
  • i feel stupid. That worked like a charm @ Carsten Løvbo Andersen, my bad. ill delete this post, saw it was marked as duplicate also. Sorry for my English, its not my native language Commented Mar 20, 2019 at 10:12

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.