Pre Exam Test Practice (SSC | Banking | IAS | GATE | State Level | CLAT | Railways | Insurance | Teaching Exams | NDA | Defence | IES |State Exam )

Organizational Research By

Surprising Reserch Topic

how to append data to div using javascript

how to append data to div using javascript  using -'javascript'

I'm using ajax to append data to DIV element, where I fill the DIV from JavaScript, how can I append new data to the DIV without losing the previous data found in DIV?

asked Oct 13, 2015 by vijaygupta1980
0 votes

Related Hot Questions

Government Jobs Opening

" rel="nofollow" target="_blank">">

and try this:

answered Oct 13, 2015 by 20shahi
0 votes

Beware of innerHTML, you sort of lose something when you use it:

theDiv.innerHTML += 'content',     

Is equivalent to:

theDiv.innerHTML = theDiv.innerHTML + 'content'

Which will destroy all nodes inside your div and recreate new ones. All references and listeners to elements inside it will be lost.

If you need to keep them (when you have attached a click handler, for example), you have to append the new contents with the DOM functions(appendChild,insertAfter,insertBefore):

var newNode = document.createElement('div');      
newNode.innerHTML = data;
theDiv.appendChild( newNode )
answered Oct 13, 2015 by okesh.badhiye