0

I have a small thing I have to make. I need to give a CSS class to a div and this has to change the insides of the div. But my button doesn't execute the onclick or something. I don't get errors in the console.

function Naampie(){
    document.getElementById("ja").className += " pasta-di-mama";
}
    
Naampie();
.pasta-di-mama{
    color: red;
    font-size: 2em;
}
<!DOCTYPE html>
<html>
<head>
    <title>Javascript Stuff</title>
</head>
<body>
    <div id="ja">
        <p>Spaghetti</p>
        <p>Macaroni</p>
        <p>MI</p>
        <p>Pasta Kip</p>
        <p>Pasta Salade</p>
  </div>

  <button onclick="Naampie()">Functie</button>

  <!--<script src="script.js" type="text/javascript"></script>-->
</body>
</html>

Does anyone have an idea why this isn't working?

3
  • 2
    You are calling the function ´Naampie()´ when your js loads. Could it be that the class is there from the beginning and you think its not being added because its already there? Commented Feb 11, 2019 at 14:07
  • 2
    Naampie(); is already called, so the div should have the class pasta-di-mama on-load... Commented Feb 11, 2019 at 14:08
  • 2
    Maybe it's just a copy-paste mistake, but you're missing a "." before the class name in your CSS. Commented Feb 11, 2019 at 14:09

5 Answers 5

3

You're missing the . in your CSS declaration to target the class. I'd also recommend using classList.add instead of appending to the className.

function Naampie() {
  document.getElementById("ja").classList.add("pasta-di-mama");
}

Naampie();
.pasta-di-mama {
  color: red;
  font-size: 2em;
}
<div id="ja">
  <p>Spaghetti</p>
  <p>Macaroni</p>
  <p>MI</p>
  <p>Pasta Kip</p>
  <p>Pasta Salade</p>
</div>

<button onclick="Naampie()">Functie</button>

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

1 Comment

also add the script tag BEFORE the closing body tag i.e INSIDE the body
1

Try classList.add(). You also forgot to precede the class with dot (.) in CSS. I will also recommend not use inline event handler:

function Naampie(){
  document.getElementById("ja").classList.add("pasta-di-mama");
}
document.getElementById('myBtn').addEventListener('click', Naampie);
.pasta-di-mama{
  color: red;
  font-size: 2em;
}
<div id="ja">
  <p>Spaghetti</p>
  <p>Macaroni</p>
  <p>MI</p>
  <p>Pasta Kip</p>
  <p>Pasta Salade</p>
</div>

<button id="myBtn">Functie</button>

Comments

0

You can use classList.add("className")

function Naampie(){
document.getElementById("ja").classList.add("pasta-di-mama");
}

Naampie();
.pasta-di-mama{
  color: red;
  font-size: 2em;
}
<!DOCTYPE html>
<html>
<head>
    <title>Javascript Shit</title>
</head>
<body>

        <div id="ja">
            <p>Spaghetti</p>
            <p>Macaroni</p>
            <p>MI</p>
            <p>Pasta Kip</p>
            <p>Pasta Salade</p>
        </div>

    <button onclick="Naampie()">Functie</button>

</body>
<script src="script.js" type="text/javascript"></script>
</html>

1 Comment

This solution looks good, but i will suggest not to execute Naampie immediately after declaration as it seems to me you are trying to add class after the button click.
0

Try add Class

function Naampie(){
document.getElementById("ja").classList.add("pasta-di-mama");
}

Comments

0

Another way of doing it...add an event listener to the btn.

document.getElementById("myBtn").addEventListener("click", function() {
  document.getElementById("ja").classList.add("pasta-di-mama");
});
.pasta-di-mama {
  color: red;
  font-size: 2em;
}
<div id="ja">
  <p>Spaghetti</p>
  <p>Macaroni</p>
  <p>MI</p>
  <p>Pasta Kip</p>
  <p>Pasta Salade</p>
</div>

<button id="myBtn">Functie</button>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.