Yes you can do the mentioned things using JavaScript:
1. Creating an element
The "tag" actually known as an element from a web developer point of view. If you refer '<p></p>' tag you are refering 'paragraph' element. The following code will demonstrate how an element can be created at run-time using JavaScript.
Code:
var newElement = document.createElement('p');
The above is the code that you needs to create a 'p' element. But you need to do something more to make this newly created element available in your page.
Code:
var newElement = document.createElement('p');
document.body.appendChild(newElement);
The second line I've added will insert the newly created 'p' element in the 'body' element of the currently loaded page at the last position. (as the last child of the 'body' element).
Now for eg: you want to put the new 'div' element inside another element whose 'ID' is "container", the following code will do the trick:
Code:
var newElement = document.createElement('div');
var containerElement = document.getElementById('container');
if(typeof containerElement !== 'undefined'){
containerElement.appendChild(newElement); //The new element will be inserted as the last child of the container element
}
2. Setting attributes/properties of a newly created element
You can do this in three different manner:
(a) Using the setAttribute method
(b) Using the dot notation
(c) Using the square bracket notation
The test here is to assign a class name 'test' to the newly created div element and want to put the new 'div' element inside another element whose 'ID' is "container":
(a)
Code:
var newElement = document.createElement('div');
newElement.setAttribute('class','test');
var containerElement = document.getElementById('container');
if(typeof containerElement !== 'undefined'){
containerElement.appendChild(newElement); //The new element will be inserted as the last child of the container element
}
Please note that if you try to use setAttribute method for setting the event handlers it will create issues in IE. So it would be better if you use any one of the 2nd or 3rd notation described here.
(b) The most frequently used form is this as this is so simple to perform the job we want.
Code:
var newElement = document.createElement('div');
newElement.className = 'test';
var containerElement = document.getElementById('container');
if(typeof containerElement !== 'undefined'){
containerElement.appendChild(newElement); //The new element will be inserted as the last child of the container element
}
Attach an event handler to the newly created div element.
Code:
var newElement = document.createElement('div');
newElement.onclick = function(){
alert('Hi its me the new one');
}
var containerElement = document.getElementById('container');
if(typeof containerElement !== 'undefined'){
containerElement.appendChild(newElement); //The new element will be inserted as the last child of the container element
}
(c)
Code:
var newElement = document.createElement('div');
newElement['className'] = 'test';
var containerElement = document.getElementById('container');
if(typeof containerElement !== 'undefined'){
containerElement.appendChild(newElement); //The new element will be inserted as the last child of the container element
}
Attach an event handler to the newly created div element.
Code:
var newElement = document.createElement('div');
newElement['onclick'] = function(){
alert('Hi its me the new one');
}
var containerElement = document.getElementById('container');
if(typeof containerElement !== 'undefined'){
containerElement.appendChild(newElement); //The new element will be inserted as the last child of the container element
}
It would be better if you can refer the following links for more details
Mozilla Developer Centre
How to create
Hope this helps
Bookmarks