CreateElement with id?

JavascriptJquery

Javascript Problem Overview


I'm trying to modify this code to also give this div item an ID, however I have not found anything on google, and idName does not work. I read something about append, however it seems pretty complicated for a task that seems pretty simple, so is there an alternative? Thanks :)

g=document.createElement('div'); g.className='tclose'; g.v=0;

Javascript Solutions


Solution 1 - Javascript

You should use the .setAttribute() method:

g = document.createElement('div');
g.setAttribute("id", "Div1");

Solution 2 - Javascript

You can use g.id = 'desiredId' from your example to set the id of the element you've created.

Solution 3 - Javascript

var g = document.createElement('div');
g.id = 'someId';

Solution 4 - Javascript

You can use Element.setAttribute

Examples:

g.setAttribute("id","yourId")

g.setAttribute("class","tclose")


Here's my function for doing this better:

function createElement(element, attribute, inner) {
  if (typeof(element) === "undefined") {
    return false;
  }
  if (typeof(inner) === "undefined") {
    inner = "";
  }
  var el = document.createElement(element);
  if (typeof(attribute) === 'object') {
    for (var key in attribute) {
      el.setAttribute(key, attribute[key]);
    }
  }
  if (!Array.isArray(inner)) {
    inner = [inner];
  }
  for (var k = 0; k < inner.length; k++) {
    if (inner[k].tagName) {
      el.appendChild(inner[k]);
    } else {
      el.appendChild(document.createTextNode(inner[k]));
    }
  }
  return el;
}

Example 1:

createElement("div");

will return this:

<div></div>

Example 2:

createElement("a",{"href":"http://google.com","style":"color:#FFF;background:#333;"},"google");`

will return this:

<a href="http://google.com" style="color:#FFF;background:#333;">google</a>

Example 3:

var google = createElement("a",{"href":"http://google.com"},"google"),
    youtube = createElement("a",{"href":"http://youtube.com"},"youtube"),
    facebook = createElement("a",{"href":"http://facebook.com"},"facebook"),
    links_conteiner = createElement("div",{"id":"links"},[google,youtube,facebook]);

will return this:

<div id="links">
    <a href="http://google.com">google</a>
    <a href="http://youtube.com">youtube</a>
    <a href="http://facebook.com">facebook</a>
</div>

You can create new elements and set attribute(s) and append child(s)

createElement("tag",{attr:val,attr:val},[element1,"some text",element2,element3,"or some text again :)"]);

There is no limit for attr or child element(s)

Solution 5 - Javascript

Why not do this with jQuery?

var newDiv= $('<div/>', { id: 'foo', class: 'tclose'})

Solution 6 - Javascript

var element = document.createElement('tagname');    

element.className= "classname";
element.id= "id";

try this you want.

Solution 7 - Javascript

I'm not sure if you are trying to set an ID so you can style it in CSS but if that's the case what you can also try:

var g = document.createElement('div');    

g.className= "g";

and that will name your div so you can target it.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionpufAmufView Question on Stackoverflow
Solution 1 - JavascriptlkaradashkovView Answer on Stackoverflow
Solution 2 - Javascriptg.d.d.cView Answer on Stackoverflow
Solution 3 - Javascriptraina77owView Answer on Stackoverflow
Solution 4 - JavascriptGuja1501View Answer on Stackoverflow
Solution 5 - JavascriptShyjuView Answer on Stackoverflow
Solution 6 - JavascriptLol SuperView Answer on Stackoverflow
Solution 7 - JavascriptJLee365View Answer on Stackoverflow