'Creating element with a class
This line of code is not working, how can I create a DOM element and adding it a class instantly?
var notes = document.createElement("div").addClass("notes card-content");
Solution 1:[1]
You're mixing jQuery and JavaScript (DOM) there. document.createElement is a DOM method and $.addClass is a jQuery one.
With jQuery, you could do:
var notes = $("<div/>").addClass("notes card-content");
Or with just the browser methods you can do:
var notes = document.createElement("div");
notes.className = "notes card-content";
Finally, you can mix them together as jQuery accepts an element as its argument:
var notes = $(document.createElement("div")).addClass("notes card-content");
Solution 2:[2]
You can't mix native methods like that with jQuery, it's either
var notes = document.createElement("div");
notes.className = 'notes card-content';
or with jQuery
var notes = $('<div />', {'class' : 'notes card-content'})
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | adeneo |
