You should use the .setAttribute() method:
g = document.createElement('div');
g.setAttribute("id", "Div1");
Answer from lkaradashkov on Stack OverflowCan't select "dynamically" created element by id
asp.net - jQuery create new div with ID? - Stack Overflow
jquery - How to add an ID onto a created element in JavaScript? - Stack Overflow
Creating a div element in jQuery [duplicate]
Just set the attribute when you're creating it
$(document.createElement('div')).attr('id', 'theID')//rest of code
I like to create the element this way:
$('<div/>',{
id: 'idhere',
css:{
width: $('#formBox').width(),
height: $('#formBox').height()
}
});
etc...
For reference - some attributes have reserved words/names (see MDN Reserved Words), so if you wanted to specify a class you need to wrap the attribute name:
$('<div/>',{
id: 'idhere',
'class':'myclassname',
css:{
width: $('#formBox').width(),
height: $('#formBox').height()
}
});
You can of course assign a name to this new element and change it as you go along...
var div = $('<div/>',{
id: 'idhere',
'class':'myclassname',
css:{
width: $('#formBox').width(),
height: $('#formBox').height()
}
});
$(div).prop('name','saymyname');
On behalf of Nik who gave the answer, here is the way you resolve this. Instead of using the DOM selector get.ElementsByClassName().innerHTML use the method newHd.innerText = newHdTxt.
use attr function to add id attribute.like this
$jqueryElement.attr('id', 'change_it_to_what_you_want_but_should_be_unique');
As of jQuery 1.4 you can pass attributes to a self-closed element like so:
jQuery('<div>', {
id: 'some-id',
class: 'some-class some-other-class',
title: 'now this div has a title!'
}).appendTo('#mySelector');
Here it is in the Docs
Examples can be found at jQuery 1.4 Released: The 15 New Features you Must Know .
You can use append (to add at last position of parent) or prepend (to add at fist position of parent):
$('#parent').append('<div>hello</div>');
// or
$('<div>hello</div>').appendTo('#parent');
Alternatively, you can use the .html() or .add() as mentioned in a different answer.
Create element and set attribute first
for (var i = 1; i <= 4; i++) {
var p= document.createElement("p");
p.setAttribute("id", "paragraph" + i);
$("body").append(p);
}
Here you go with the solution https://jsfiddle.net/7snh10zz/
for (var i = 1; i <= 4; i++) {
$("body").append("<p id='paragraph" + i + "'>Paragraph " + i + "</p>");
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
One more way to do it https://jsfiddle.net/7snh10zz/2/
for (var i = 1; i <= 4; i++) {
$("body").append(document.createElement("p"));
$("p:last-child").attr('id', 'paragraph' + i).text("Paragraph " + i);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
The first option gives you more flexibilty:
var $div = $("<div>", {id: "foo", "class": "a"});
$div.click(function(){ /* ... */ });
$("#box").append($div);
And of course .html('*') overrides the content while .append('*') doesn't, but I guess, this wasn't your question.
Another good practice is prefixing your jQuery variables with $:
Is there any specific reason behind using $ with variable in jQuery
Placing quotes around the "class" property name will make it more compatible with less flexible browsers.
I personally think that it's more important for the code to be readable and editable than performant. Whichever one you find easier to look at and it should be the one you choose for above factors. You can write it as:
$('#box').append(
$('<div/>')
.attr("id", "newDiv1")
.addClass("newDiv purple bloated")
.append("<span/>")
.text("hello world")
);
And your first Method as:
// create an element with an object literal, defining properties
var $e = $("<div>", {id: "newDiv1", name: 'test', class: "aClass"});
$e.click(function(){ /* ... */ });
// add the element to the body
$("#box").append($e);
But as far as readability goes; the jQuery approach is my favorite. Follow this Helpful jQuery Tricks, Notes, and Best Practices
You can append img to div before you append that div to li. You can also write the same code like this Fiddle
$('<li/>')
.append($('<div/>', {
'class': 'img-w js-popup',
}).append($('<img/>', {
'src': '//www.gravatar.com/avatar/9142888a2ae6160f2faec90134eeafa3/?default=&s=80'
})))
.append($('<a/>', {
href: 'delete.php',
text: 'Delete'
}))
.appendTo('ul');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul>
<li>
<div>
<img src="//www.gravatar.com/avatar/9142888a2ae6160f2faec90134eeafa3/?default=&s=80" alt="" />
</div>
<a href="delete.php">Delete</a>
</li>
</ul>
The issue is because your append() call is on the li itself, not the div you just added. Try this:
var $div = $('<div/>', {
'class': 'img-w js-popup'
});
$('<img/>', {
'src': '//www.gravatar.com/avatar/9142888a2ae6160f2faec90134eeafa3/?default=&s=80'
}).appendTo($div)
$('<li/>').append($div).append($('<a/>', {
href: 'delete.php',
text: 'Delete'
})).appendTo('ul');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<ul>
<li>
<div>
<img src="//www.gravatar.com/avatar/9142888a2ae6160f2faec90134eeafa3/?default=&s=80" alt="" />
</div>
<a href="delete.php">Delete</a>
</li>
</ul>
With that said, you're using the most verbose method of creating HTML. As you're hard-coding all attributes, it can be simplified by just providing the plain string - or even by just cloning the previous li and appending that.