So for my code that i have below the reg.onclick is happening before it is even clicked and I'm not really sure how to fix this. Thanks for the help in advance.
function displaySearch(){
var div = document.getElementById('table')
var table = document.getElementById('tableSearch')
var res = xmlhttp.responseXML
var e = res.getElementsByTagName('element')
for(var i = 0; i < e.length; i++){
var splitResult = e[i].textContent.split('#')
var course = splitResult[0]
var num = splitResult[1]
var tut = splitResult[2]
var email = splitResult[3]
var capacity = splitResult[4]
var users = splitResult[5]
var registered = users.split('/')
var rowCount = table.rows.length
var row = table.insertRow(rowCount)
var cell1 = row.insertCell(-1)
cell1.id = "1" + i
cell1.innerHTML = course
var cell2 = row.insertCell(-1)
cell2.id = "2" + i
cell2.innerHTML = num
var cell3 = row.insertCell(-1)
cell3.id = "3" + i
cell3.innerHTML = tut
var cell4 = row.insertCell(-1)
cell4.id = "4" + i
cell4.innerHTML = email
var cell5 = row.insertCell(-1)
cell5.id = "5" + i
cell5.innerHTML = capacity
var cell6 = row.insertCell(-1)
cell6.id = "6" + i
if(users != ""){
for(var i = 0; i < registered.length; i++){
var p = "

" + registered[i] + "

"
cell6.innerHTML = cell6.innerHTML + p
}
}
else{
cell6.innerHTML = ""}
var cell7 = row.insertCell(-1)
cell7.id = "7" + i
var reg = document.createElement('input')
reg.type = 'button'
reg.value = "Register"
reg.id = i
reg.name = i
var createReg = function(arg){registerCourse(arg)}
reg.onclick = createReg(cell7.id)
var cell = "row"+i
cell7.appendChild(reg)
}
}

Member Avatar for stbuchok

Please remember to put semicolons at the end of your lines in JavaScript.

Also, you are not making a reference to a function, you are executing that function, then making a reference to it's results.

You should have the following:

reg.onclick = function(){...};

instead you are doing the following:

reg.onclick = function(){...}();

the second set of () brackets execute the function, this is what you are doing.

a quick patch:

    reg.onclick = function(){createReg(cell7.id)}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.