...

View Full Version : onClick is getting overwritten while dynamically creatiing elements using javascript



naveenkn
12-26-2008, 11:44 AM
Hi All,
I am developing a news portlet.In this user can vote the news content for priority.Which news content needs to be showed first or last is calculated depending the number of votes. When user increments the news content priority i am making an ajax which updates the database column and creates the news new content area by changing the order of priority depending on the number of votes..

First time when user cast the vote i am able to generate dynamically all the webpage contents using javascript. But the problem is onclick function is picking the url which i am assigning for the last news content while iterating the news content. So when i clicked for updating the priority, last news content priority is getting incremented.

I am created the priority image is as follows::


function createTable()
{

var newsEl = document.createElement("TABLE");
newsEl.setAttribute("cellspacing", "0");
newsEl.setAttribute("cellpadding", "0");
newsEl.setAttribute("width", "554");
newsEl.setAttribute("border", "0");

var newsList= xmlDoc.getElementsByTagName('newsList');
var x = newsList[0].getElementsByTagName('news');

var tmp = document.createElement('TBODY');
newsEl.appendChild(tmp);


for (i=0;i<x.length;i++)
{
var row = document.createElement('TR');
var newsHeader=x[i].getElementsByTagName("description")[0].childNodes[0].nodeValue;
var newsContent=x[i].getElementsByTagName("content")[0].childNodes[0].nodeValue;
var newsId=x[i].getElementsByTagName("id")[0].childNodes[0].nodeValue;


var newsLink=x[i].getElementsByTagName("newsLink")[0].childNodes[0].nodeValue;
var contentHeaderTD = document.createElement('TD');
contentHeaderTD.style.backgroundColor = "#CFECEC";
contentHeaderTD.setAttribute("bgcolor", "#004080");
var contentHeader =document.createElement('div');

var plusImage = document.createElement("img");
plusImage.setAttribute('src', '../img/plus.jpg');
plusImage.setAttribute('alt', "../priority.do?value=increment&id="+newsId);
plusImage.setAttribute('height', '30px');
plusImage.setAttribute('width', '30px');
plusImage.setAttribute('id','plus');
var plusUrl="../priority.do?value=increment&id="+newsId;

plusImage.onclick= function(){
postRequest(plusUrl);
};







contentHeaderTD.appendChild(plusImage);

var minusImage= document.createElement("img");

minusImage.setAttribute('id','minus');
minusImage.setAttribute('src', '../img/minus.jpg');
minusImage.setAttribute('alt', "../priority.do?value=decrement&id="+newsId);
minusImage.setAttribute('height', '30px');
minusImage.setAttribute('width', '30px');




var minusurl="../priority.do?value=decrement&id="+newsId;

minusImage.onclick= function(){
postRequest(minusurl);
};



contentHeaderTD.appendChild(minusImage);
var contTD = document.createElement('td');
contTD.innerHTML='<font color="#3300F"><a href="../brief.do?id='+newsId+'">'+newsHeader+'</a></h3>';

contentHeaderTD.appendChild(contTD);
row.appendChild(contentHeaderTD);

tmp.appendChild(row);
var rowData = document.createElement('TR');

var contentTD = document.createElement('TD');

contentTD.style.backgroundColor = "#FFFFCC";
contentTD.setAttribute("align","justify");

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

contentDiv.innerHTML ='<tr><td bgcolor="#FFFFCC" align="justify" >'+newsContent+'&nbsp;'+'<a href="../brief.do?id='+newsId+'">'+'more...'+'</a>'+'</td></tr><tr><td>&nbsp;</td></tr>';
contentTD.appendChild(contentDiv);
rowData.appendChild(contentTD);
tmp.appendChild(rowData);


var dummyDiv=document.createElement('div');
dummyDiv.innerHTML ='<tr><td>&nbsp;</td></tr>';
var dummyTD = document.createElement('TD');
dummyTD.appendChild(dummyDiv);
var DummyrowData = document.createElement('TR');
DummyrowData.appendChild(dummyTD);
tmp.appendChild(DummyrowData);

}
removeElement();
var newdiv = document.createElement('newsDisplay');
newdiv.setAttribute('id','newsDisplay');
var newsData = document.getElementById('newsData');
newdiv.appendChild(newsEl);
newsData.appendChild(newdiv);

}


can some body put some light for the solution to this problem..

Thanks in Advance..


Regards
Naveen

naveenkn
12-29-2008, 06:32 AM
Hi everyone,
Can any one please give a solution to my problem???

Thanks in advance
Naveen



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum