1

I try to add class in javascript to div, but it doesnt work.

Here's the div where I want to add class on it:

postimage.style.container = {"margin-bottom": "0.5em", "margin-top": "0.5em"};

I try to container.className += ' additionalClass'; or container_div.setAttribute('class',"classname") but it doesn't work It's div and code in browser looks like https://image.prntscr.com/image/FGSl9DXvSQybCEeYx2qIDA.png

Full plugin code: https://postimages.org/plugins

I tried this, but it doesn't work for me Change an element's class with JavaScript

Penny
  • 11
  • 2
  • Where is container coming from? How are you initially adding that div to the page? It would help a lot if you could add an Id to it, then it would be very easy to grab the element later in javascript. – Tyler Dahle Jul 13 '17 at 19:36
  • @TylerDahle here is full code od this http://mod.postimage.org/mybb.js – Penny Jul 13 '17 at 19:40
  • You can post code in here so that it is formatted and easier to read. But when you create the div, you should then attach an Id to it, you won't need to pass it around everywhere. You could then just do var container = document.getElementById('myDiv'); whenever you need to access it. Then container.className = 'my-class'; – Tyler Dahle Jul 13 '17 at 19:47
  • @TylerDahle This div is created "automatically" in code and I do not know where I can find id and add class – Penny Jul 13 '17 at 19:54
  • I saw this in that code when ctrl+f and searched for container: var container=document.createElement('div'); You have to insert it into the DOM after creating it if you are not doing that somewhere. Do you not have access to this code to modify/edit? – Tyler Dahle Jul 13 '17 at 19:57
  • Code about it https://codepaste.net/vzbinn and http://postimages.org/mybb plugin documentation – Penny Jul 13 '17 at 19:57
  • Ok, so you are just trying to do that little customization function that they give a little demo of on that page? If that is the case, what do you get when you console.log(postimage). It is most likely a custom object so you can't just apply typical properties of DOM elements to it like className and setAttribute. – Tyler Dahle Jul 13 '17 at 20:07
  • I can edit this code how I want, but i dont know how to add class on this element `var container=document.createElement('div');` – Penny Jul 13 '17 at 20:21
  • You can do container.setAttribute('id', 'myId') after creating the element. since Id is an attribute. Then, whenever you need that element, you can do var ele = document.getElementById(myId);. Then you should be able to more easily apply a class to it. Unless you only need to apply a class once initially, then you can just do container.className = 'myClass' after creating it without needing an Id. – Tyler Dahle Jul 13 '17 at 20:24
  • Yes i did it when you wrote it :) Thanks for help! It's work as well. https://stackoverflow.com/questions/1115310/how-to-add-a-class-to-dom-element-in-javascript (info) – Penny Jul 13 '17 at 20:26
  • Np, glad you got it! – Tyler Dahle Jul 13 '17 at 20:27

0 Answers0