onclick function executes without any click on javascript

alex05

New Member
I am working with some Javascript and Ajax functions. I am going to put some code while I explain myself to be more clear.I have this element: \[code\]<div id="divTest" onclick="test()">YES</div>\[/code\]When the user clicks the DIV, the function replaces the "YES" for something like this:\[code\]YES <input type="radio" name="testing" value="http://stackoverflow.com/questions/13810950/YES" onclick="test_2(this.value)" checked /> NO <input type="radio" name="testing" value="http://stackoverflow.com/questions/13810950/NO" onclick="test_2(this.value)" />\[/code\]This allows the user to select again the right option. Then I want to replace the radiobuttons with new value, so when the user selects any option, the DIV replaces the "radio" options and displays the value selected (YES OR NO) as it was on the begining.At this point everything works perfect. Here is my problem:I want the DIV to have the \[code\]onclick()\[/code\] as the begining \[code\]onclick="test()"\[/code\] so I do it from javascript giving it the property this way: \[code\]div.onclick = function() { test();};\[/code\]. The function \[code\]test()\[/code\] is executed even if the user does not click on the div. It executes both function right away and does not wait until there is a click on it.Does anyone knows how can I make the function to wait until there is any click on it? Am I giving the onclick property incorrectly?I hope I made myself clear. (Sorry for my english)Here are the functions\[code\]function test() { var div = document.getElementById("divTest"); //I DO NOT INCLUDE THE AJAX CODE BUT THE RESPONSE INCLUDE THE RADIO BUTTONS METIONED ABOVE div.innerHTML = xml.responseText();}function test_2(newValue) { div = document.getElementById("newValue"); div.innerHTML = newValue; div.onclick = function() { test(); };}\[/code\]
 
Back
Top