1

I want to apply the style form my CSS file to a HTML class which I created with Javascript.

My Code looks like this `

    socket.on('chat', function (data) {
        feedback.innerHTML = '';
        var mewTime = document.createElement("span")
        mewTime.class = "chatTime";
        mewTime.text = data.currentTime;

        console.log(mewTime);

        var userName = createUser(data, mewTime);

        var content = document.createElement("span")
        content.class = "messageContent"
        content.append(data.messageInput)

        var message = document.createElement("div")
        message.class = "message";
        message.append(userName[0].outerHTML);
        message.append(mewTime);
        message.append(document.createElement("br"))
        message.append(content);

        output.append(message);

        currentTime = '';
        messageInput.focus();
    });

function createUser(data, time){
    if(time === null){
        var userName = $('<span class="userNameOnline"/>');
        userName.text(data);  
    } else{
        var userName = $('<span class="userName"/>')
            .text(data.username + ' ' + time.text);
    }
    console.log(userName);
    return userName;
}

// CSS
.message{
color: red;
}

.userName {
color: blue;
}

`

I can access the text in the message but I can't style it with my CSS.

I tried to use .outerHTML, .innerHTML, .text. On the console it shows that the class is correctly set.

5
  • w3schools.com/js/js_htmldom_css.asp Commented Mar 19, 2019 at 11:23
  • Please provide a minimal reproducible example Commented Mar 19, 2019 at 11:26
  • 1
    Which element do you want to change? Have you tried element.className = "message"? Commented Mar 19, 2019 at 11:27
  • But isn't it possible that my style.css file applys the style to it? Commented Mar 19, 2019 at 11:27
  • developer.mozilla.org/en-US/docs/Web/API/Element/classList classList is the html5 way to do it. Otherwise you should use the className property as nick proposed. This way the style of your css will be applied to the element. Commented Mar 19, 2019 at 11:31

7 Answers 7

2

You're looking for classList.add('') instead of class = ''. You can find more information here.

Here's a basic example:

   
var message = document.createElement("div")
message.classList.add("message")


document.getElementById("output").append(message);
.message {
  height: 200px;
  width: 200px;
  background: blue;
}
<div id="output"></div>

In your code you can change it to the following and it should work:

var message = document.createElement("div")
message.classList.add("message")
message.append(userName[0].outerHTML);
message.append(mewTime);
message.append(document.createElement("br"))
message.append(content);

output.append(message);
Sign up to request clarification or add additional context in comments.

Comments

1

You can call .setAttribute("attributeName", "value of attribute") or you can also use .className instead of a normal class. So for your example we can have something like:

var mewTime = document.createElement("span");
mewTime.setAttribute("class", "chatTime");

OR

var mewTime = document.createElement("span");
mewTime.className = "chatTime"

Comments

1

$(document).ready(function() {
    document.getElementById("divTest").classList.add('red');
});
.red {
  background-color: red;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<html>
  <head>
    
  </head>
  <body>
    <div id='divTest'>
      test
    </div>
  </body>
</html>

You should use classList: https://developer.mozilla.org/en-US/docs/Web/API/Element/classList

Comments

0

You could use as below example to apply CSS on HTML element in Javascript:

yourElement.style.border= "1px solid red";

2 Comments

Thank you. But i want to have all my styling in one file and not in my script file and my css file. Is this possiple?
Yes why not, but I didnt understand what you want to exactly.
0

Following is the method to do that.

var newElement = document.createElement("p");
newElement.setAttribute("class", "className");

Comments

0

You can do like this

function myFunction() {
var message = document.createElement("span");
message.innerHTML = "Test"
        message.className = "message";
        
         document.body.appendChild(message);
}
.message{
color:red;
}
<!DOCTYPE html>
<html>
<body>

<p>Click the button to make element.</p>

<button onclick="myFunction()">Create</button>

<script>

</script>

</body>
</html>

Comments

-1

Just change class to className, I hope it'll help you out. Thanks

    mewTime.className = "chatTime";

    content.className = "messageContent"

    message.className = "message";

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.